Java Code Examples for com.icegreen.greenmail.util.GreenMailUtil#sendAttachmentEmail()

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#sendAttachmentEmail() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LargeMessageTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testLargeMessageTextAndAttachment() throws MessagingException, IOException {
    String to = "to@localhost";
    GreenMailUtil.sendAttachmentEmail(to, "from@localhost", "Subject", createLargeString(),
            createLargeByteArray(), "application/blubb", "file", "descr",
            greenMail.getSmtp().getServerSetup());
    greenMail.waitForIncomingEmail(5000, 1);

    retrieveAndCheck(greenMail.getPop3(), to);
    retrieveAndCheck(greenMail.getImap(), to);
}
 
Example 2
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmtpServerReceiveMultipart() throws Exception {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    GreenMailUtil.sendAttachmentEmail("[email protected]", "[email protected]", subject, body, new byte[]{0, 1, 2}, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(1500, 1);
    Message[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());

    Object o = emails[0].getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart mp = (MimeMultipart) o;
    assertEquals(2, mp.getCount());
    BodyPart bp;
    bp = mp.getBodyPart(0);
    assertEquals(body, GreenMailUtil.getBody(bp).trim());

    bp = mp.getBodyPart(1);
    assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GreenMailUtil.copyStream(bp.getInputStream(), bout);
    byte[] gif = bout.toByteArray();
    for (int i = 0; i < gif.length; i++) {
        assertEquals(i, gif[i]);
    }
}
 
Example 3
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetriveMultipart() throws Exception {
    assertNotNull(greenMail.getImap());

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    String to = "test@localhost";
    GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, new byte[]{0, 1, 2}, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        Message[] messages = retriever.getMessages(to);

        Object o = messages[0].getContent();
        assertTrue(o instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) o;
        assertEquals(2, mp.getCount());
        BodyPart bp;
        bp = mp.getBodyPart(0);
        assertEquals(body, GreenMailUtil.getBody(bp).trim());

        bp = mp.getBodyPart(1);
        assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GreenMailUtil.copyStream(bp.getInputStream(), bout);
        byte[] gif = bout.toByteArray();
        for (int i = 0; i < gif.length; i++) {
            assertEquals(i, gif[i]);
        }
    }
}
 
Example 4
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveMultipart() throws Exception {
    assertNotNull(greenMail.getPop3());

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    String to = "[email protected]";
    GreenMailUtil.sendAttachmentEmail(to, "[email protected]", subject, body, new byte[]{0, 1, 2}, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        Message[] messages = retriever.getMessages(to);

        Object o = messages[0].getContent();
        assertTrue(o instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) o;
        assertEquals(2, mp.getCount());
        BodyPart bp;
        bp = mp.getBodyPart(0);
        assertEquals(body, GreenMailUtil.getBody(bp).trim());

        bp = mp.getBodyPart(1);
        assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GreenMailUtil.copyStream(bp.getInputStream(), bout);
        byte[] gif = bout.toByteArray();
        for (int i = 0; i < gif.length; i++) {
            assertEquals(i, gif[i]);
        }
    }
}
 
Example 5
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopContent() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL, "pop3",
          EmailReader.PARAM_WAIT, 5,
          EmailReader.PARAM_SERVER, greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT, greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER, "[email protected]",
          EmailReader.PARAM_PASS, "password",
          EmailReader.PARAM_PROCESS, "content");

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();
}
 
Example 6
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 7
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopBoth() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "both",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 8
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopDeleteMessages() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "content",
          EmailReader.PARAM_DELETE_EMAIL,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  // Check that there are no messages on the server
  bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password");

  bcr.initialize();
  assertFalse(bcr.doHasNext());
  bcr.close();
}
 
Example 9
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopDeleteAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath(),
          EmailReader.PARAM_DELETE_ATTACHMENT,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(0, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 10
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopBadProcessConfig() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "NotARealProcess",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 11
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapContent() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL, "imap",
          EmailReader.PARAM_WAIT, 5,
          EmailReader.PARAM_SERVER, greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT, greenMail.getImap().getPort(),
          EmailReader.PARAM_USER, "[email protected]",
          EmailReader.PARAM_PASS, "password",
          EmailReader.PARAM_PROCESS, "content");

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();
}
 
Example 12
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 13
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapBoth() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "both",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 14
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapDeleteMessages() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "content",
          EmailReader.PARAM_DELETE_EMAIL,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  // Check that there are no messages on the server
  bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password");

  bcr.initialize();
  assertFalse(bcr.doHasNext());
  bcr.close();
}
 
Example 15
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapDeleteAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath(),
          EmailReader.PARAM_DELETE_ATTACHMENT,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(0, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 16
Source File: GreenMailUtilTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetEmptyBodyAndHeader() throws Exception {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        String subject = GreenMailUtil.random();
        String body = ""; // Provokes https://github.com/greenmail-mail-test/greenmail/issues/151
        String to = "test@localhost";
        final byte[] gifAttachment = {0, 1, 2};
        GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, gifAttachment,
                "image/gif", "testimage_filename", "testimage_description",
                greenMail.getSmtp().getServerSetup());
        greenMail.waitForIncomingEmail(5000, 1);

        try (Retriever retriever = new Retriever(greenMail.getImap())) {
            MimeMultipart mp = (MimeMultipart) retriever.getMessages(to)[0].getContent();
            BodyPart bp;
            bp = mp.getBodyPart(0);
            assertEquals(body, GreenMailUtil.getBody(bp).trim());
            assertEquals(
                    "Content-Type: text/plain; charset=us-ascii\r\n" +
                    "Content-Transfer-Encoding: 7bit",
                    GreenMailUtil.getHeaders(bp).trim());

            bp = mp.getBodyPart(1);
            assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());
            assertEquals(
                    "Content-Type: image/gif; name=testimage_filename\r\n" +
                    "Content-Transfer-Encoding: base64\r\n" +
                    "Content-Disposition: attachment; filename=testimage_filename\r\n" +
                    "Content-Description: testimage_description",
                    GreenMailUtil.getHeaders(bp).trim());

            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            GreenMailUtil.copyStream(bp.getInputStream(), bout);
            assertArrayEquals(gifAttachment, bout.toByteArray());
        }
    } finally {
        greenMail.stop();
    }
}