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

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#sendTextEmailTest() . 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: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 6 votes vote down vote up
@Test
public void pollSingleMail() throws MessagingException {
  greenMail.setUser("[email protected]", "bpmn");

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "text body");

  PollMailResponse response = MailConnectors.pollMails()
    .createRequest()
      .folder("INBOX")
    .execute();

  List<Mail> mails = response.getMails();
  assertThat(mails).hasSize(1);

  Mail mail = mails.get(0);
  assertThat(mail.getSubject()).isEqualTo("subject");
  assertThat(mail.getText()).isEqualTo("text body");
}
 
Example 2
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetrieveWithNonDefaultPassword() throws Exception {
    assertNotNull(greenMail.getPop3());
    final String to = "[email protected]";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "[email protected]", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        try {
            retriever.getMessages(to, "wrongpassword");
            fail("Expected authentication failure");
        } catch (Throwable e) {
            // ok
        }

        Message[] messages = retriever.getMessages(to, password);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());
    }
}
 
Example 3
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testPopWait() throws Exception {
  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();

  assertFalse(bcr.doHasNext());
  GreenMailUtil.sendTextEmailTest(
      "[email protected]", "[email protected]", GreenMailUtil.random(), GreenMailUtil.random());
  assertFalse(bcr.doHasNext()); // Should be a 5 second delay before it returns true

  Thread.sleep(5000);

  assertTrue(bcr.doHasNext());

  bcr.close();
}
 
Example 4
Source File: AuthenticationDisabledTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMailAndReceiveWithAuthDisabled() throws MessagingException, IOException {
    final String to = "to@localhost";
    final String subject = "subject";
    final String body = "body";
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());
    assertEquals(body, GreenMailUtil.getBody(emails[0]));

    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        Message[] messages = retriever.getMessages(to);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, messages[0].getContent());
    }
}
 
Example 5
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetreiveSimpleWithNonDefaultPassword() throws Exception {
    assertNotNull(greenMail.getImap());
    final String to = "[email protected]";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        try {
            retriever.getMessages(to, "wrongpassword");
            fail("Expected failed login");
        } catch (Throwable e) {
            // ok
        }

        Message[] messages = retriever.getMessages(to, password);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, ((String) messages[0].getContent()).trim());
    }
}
 
Example 6
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 6 votes vote down vote up
@Test
public void pollMultipleMails() throws MessagingException {
  greenMail.setUser("[email protected]", "bpmn");

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "mail-1", "body");
  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "mail-2", "body");

  PollMailResponse response = MailConnectors.pollMails()
    .createRequest()
      .folder("INBOX")
    .execute();

  List<Mail> mails = response.getMails();
  assertThat(mails)
    .hasSize(2)
    .extracting("subject")
    .contains("mail-1", "mail-2");
}
 
Example 7
Source File: ExampleReceiveTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testReceive() throws MessagingException {
    GreenMailUser user = greenMail.setUser("[email protected]", "login-id", "password");
    user.deliver(createMimeMessage()); // You can either create a more complex message...
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]",
            "subject", "body"); // ...or use the default messages

    assertEquals(2, greenMail.getReceivedMessages().length); // // --- Place your POP3 or IMAP retrieve code here
}
 
Example 8
Source File: SenderRecipientTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendAndReceiveWithQuotedAddress() throws MessagingException, IOException {
    // See https://en.wikipedia.org/wiki/Email_address#Local-part
    String[] toList = {"\"John..Doe\"@localhost",
            "abc.\"defghi\".xyz@localhost",
            "\"abcdefghixyz\"@localhost",
            "\"Foo Bar\"admin@localhost"
    };
    for(String to: toList) {
        greenMail.setUser(to, "pwd");
        InternetAddress[] toAddress = InternetAddress.parse(to);
        String from = to; // Same from and to address for testing correct escaping of both

        final String subject = "testSendAndReceiveWithQuotedAddress";
        final String content = "some body";
        GreenMailUtil.sendTextEmailTest(to, from,
                subject, content);

        assertTrue(greenMail.waitForIncomingEmail(5000, 1));

        final IMAPStore store = greenMail.getImap().createStore();
        store.connect(to, "pwd");
        try {
            IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] msgs = folder.getMessages();
            assertTrue(null != msgs && msgs.length == 1);
            final Message msg = msgs[0];
            assertEquals(to, ((InternetAddress)msg.getRecipients(Message.RecipientType.TO)[0]).getAddress());
            assertEquals(from, ((InternetAddress)msg.getFrom()[0]).getAddress());
            assertEquals(subject, msg.getSubject());
            assertEquals(content, msg.getContent().toString());
            assertArrayEquals(toAddress, msg.getRecipients(Message.RecipientType.TO));
        } finally {
            store.close();
        }
    }
}
 
Example 9
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 5 votes vote down vote up
@Test
public void folderFromConfiguration() throws MessagingException {
  greenMail.setUser("[email protected]", "bpmn");

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "text body");

  PollMailResponse response = MailConnectors.pollMails()
    .createRequest()
    .execute();

  List<Mail> mails = response.getMails();
  assertThat(mails).hasSize(1);
}
 
Example 10
Source File: MultipleSetupsTests.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Receive test")
void testReceive() {
    greenMail.setUser("[email protected]", "login-id", "password");
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "body");
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "body");
    final MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(2, emails.length);
}
 
Example 11
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testPopLongWait() throws Exception {
  String subject = GreenMailUtil.random();
  String body = GreenMailUtil.random();

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

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject, body);
  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject2, body2);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL, "pop3",
          EmailReader.PARAM_WAIT, 15,
          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);

  jCas.reset();

  Thread.sleep(20000);

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

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();
}
 
Example 12
Source File: DefaultSetupTests.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Send test")
void testSend() throws MessagingException {
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "body");
    final MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    final MimeMessage email = emails[0];
    assertEquals("subject", email.getSubject());
    assertEquals("body", GreenMailUtil.getBody(email));
}
 
Example 13
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmtpServerBasic() throws MessagingException {
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "subject", "body");
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals("subject", emails[0].getSubject());
    assertEquals("body", GreenMailUtil.getBody(emails[0]));
}
 
Example 14
Source File: SenderRecipientTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendWithoutSubject() {
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]",
            null, "some subject less body");
    assertEquals("some subject less body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
}
 
Example 15
Source File: MultiRequestTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
public void run() {
    for (int i = 0; i < count; i++) {
        GreenMailUtil.sendTextEmailTest(to, "[email protected]", "subject", "body");
    }
    finished = true;
}
 
Example 16
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 17
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 18
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 19
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 20
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);
}