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

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#sendMimeMessage() . 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 7 votes vote down vote up
@Test
public void htmlMessage() throws MessagingException {
  greenMail.setUser("[email protected]", "bpmn");

  Session session = greenMail.getSmtp().createSession();
  MimeMessage message = MailTestUtil.createMimeMessageWithHtml(session);

  GreenMailUtil.sendMimeMessage(message);

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

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

  Mail mail = mails.get(0);
  assertThat(mail.getHtml()).isEqualTo("<b>html</b>");
  assertThat(mail.getText()).isEqualTo("text");
}
 
Example 2
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 6 votes vote down vote up
@Test
public void messageWithAttachment() throws Exception {
  File attachment = new File(getClass().getResource("/attachment.txt").toURI());
  assertThat(attachment.exists()).isTrue();

  greenMail.setUser("[email protected]", "bpmn");

  Session session = greenMail.getSmtp().createSession();
  MimeMessage message = MailTestUtil.createMimeMessageWithAttachment(session, attachment);
  GreenMailUtil.sendMimeMessage(message);

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

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

  Mail mail = mails.get(0);
  assertThat(mail.getAttachments()).hasSize(1);

  Attachment mailAttachment = mail.getAttachments().get(0);
  assertThat(mailAttachment.getFileName()).isEqualTo("attachment.txt");
  assertThat(mailAttachment.getPath()).isNotNull();
}
 
Example 3
Source File: EscapingTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscapeMessageID() throws MessagingException, IOException {
    String to = "foo@localhost";
    String from = "bar`bar <bar@localhost>";
    String subject = "Bad IMAP Envelope";
    String body = "Example text";
    greenMail.setUser(to, to);

    Session smtpSession = greenMail.getSmtp().createSession();
    GreenMailMimeMessage mimeMessage = new GreenMailMimeMessage(smtpSession);

    mimeMessage.setRecipients(Message.RecipientType.TO, to);
    mimeMessage.setFrom(from);
    mimeMessage.setSubject(subject);
    mimeMessage.setText(body);

    GreenMailUtil.sendMimeMessage(mimeMessage);
    greenMail.waitForIncomingEmail(5000, 1);

    retrieveAndCheck(greenMail.getImap(), to, from, subject);
}
 
Example 4
Source File: DateTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testDatesCorrect() throws MessagingException, IOException {
    String to = "to@localhost";
    greenMail.setUser(to, to);

    // Create mail with specific 'sent' date
    final MimeMessage mail = GreenMailUtil.createTextEmail(to, "from@localhost", "Subject", "msg", greenMail.getSmtp().getServerSetup());
    final Date sentDate = new GregorianCalendar(2000, 1, 1, 0, 0, 0).getTime();
    mail.setSentDate(sentDate);
    GreenMailUtil.sendMimeMessage(mail);

    greenMail.waitForIncomingEmail(5000, 1);

    retrieveAndCheck(greenMail.getPop3(), to, sentDate, false);
    retrieveAndCheck(greenMail.getImap(), to, sentDate, true);
}
 
Example 5
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendAndWaitForIncomingMailsInBcc() throws Throwable {
    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    final MimeMessage message = createTextEmail("test@localhost", "from@localhost", subject, body, greenMail.getSmtp().getServerSetup());
    message.addRecipients(Message.RecipientType.BCC, "bcc1@localhost,bcc2@localhost");

    assertEquals(0, greenMail.getReceivedMessages().length);

    GreenMailUtil.sendMimeMessage(message);

    assertTrue(greenMail.waitForIncomingEmail(1500, 3));

    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(3, emails.length);
}
 
Example 6
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSmtpServerReceiveWithAUTHSuffix() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();

    Properties mailProps = new Properties();
    mailProps.setProperty("mail.smtp.from", "<[email protected]> AUTH <somethingidontknow>");
    Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, mailProps);

    MimeMessage message = new MimeMessage(session);
    message.setContent("body1", "text/plain");
    message.setFrom("from@localhost");
    message.setRecipients(RecipientType.TO, InternetAddress.parse("to@localhost"));
    message.setSubject(subject);

    GreenMailUtil.sendMimeMessage(message);
    System.setProperty("mail.smtp.from", "<[email protected]> AUTH <somethingidontknow>");
    

    greenMail.waitForIncomingEmail(1500, 1);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());
}
 
Example 7
Source File: SendReceiveWithInternationalAddressTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSend() throws MessagingException, UnsupportedEncodingException {

    Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, properties);
    MimeMessage mimeMessage = new MockInternationalizedMimeMessage(session);
    mimeMessage.setSubject("subject");
    mimeMessage.setSentDate(new Date());
    mimeMessage.setFrom("múchätįldé@tìldę.oœ");
    mimeMessage.setRecipients(Message.RecipientType.TO, "用户@例子.广告");
    mimeMessage.setRecipients(Message.RecipientType.CC, "θσερεχα@μπλε.ψομ");
    mimeMessage.setRecipients(Message.RecipientType.BCC, "राममो@हन.ईन्फो");

    // The body text needs to be encoded if it contains non us-ascii characters
    mimeMessage.setText(MimeUtility.encodeText("用户@例子"));

    GreenMailUtil.sendMimeMessage(mimeMessage);

    // Decoding the body text to verify equality
    String decodedText = MimeUtility.decodeText(GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
    assertEquals("用户@例子", decodedText);
}
 
Example 8
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 5 votes vote down vote up
@Test
public void messageHeaders() throws Exception {
  greenMail.setUser("[email protected]", "bpmn");

  Session smtpSession = greenMail.getSmtp().createSession();

  MimeMessage message = new MimeMessage(smtpSession);
  message.setFrom(new InternetAddress("[email protected]"));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
  message.addRecipient(Message.RecipientType.CC, new InternetAddress("[email protected]"));
  message.setSubject("subject");
  message.setText("body");

  GreenMailUtil.sendMimeMessage(message);

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

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

    Mail mail = mails.get(0);
    assertThat(mail.getFrom()).isEqualTo("[email protected]");
    assertThat(mail.getTo()).isEqualTo("[email protected]");
    assertThat(mail.getCc()).isEqualTo("[email protected]");
    assertThat(mail.getSubject()).isEqualTo("subject");
    assertThat(mail.getSentDate()).isNotNull();
    assertThat(mail.getReceivedDate()).isNotNull();
    assertThat(mail.getMessageNumber()).isEqualTo(1);
    assertThat(mail.getMessageId()).isNotNull();
}
 
Example 9
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 5 votes vote down vote up
@Test
public void messageWithSingleAttachmentOnly() throws Exception {
  greenMail.setUser("[email protected]", "bpmn");

  Session smtpSession = greenMail.getSmtp().createSession();
  MimeMessage message = MailTestUtil.createMimeMessage(smtpSession);

  message.setContent("text body", MailContentType.TEXT_PLAIN.getType());
  message.setFileName("attachment.txt");
  message.setDisposition(Part.ATTACHMENT);

  GreenMailUtil.sendMimeMessage(message);

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

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

  Mail mail = mails.get(0);
  assertThat(mail.getAttachments()).hasSize(1);

  Attachment mailAttachment = mail.getAttachments().get(0);
  assertThat(mailAttachment.getFileName()).isEqualTo("attachment.txt");
  assertThat(mailAttachment.getPath()).isNotNull();
}
 
Example 10
Source File: PollMailConnectorTest.java    From camunda-bpm-mail with Apache License 2.0 5 votes vote down vote up
@Test
public void messageWithAttachmentNoDownload() throws Exception {
  File attachment = new File(getClass().getResource("/attachment.txt").toURI());
  assertThat(attachment.exists()).isTrue();

  greenMail.setUser("[email protected]", "bpmn");

  Session session = greenMail.getSmtp().createSession();
  MimeMessage message = MailTestUtil.createMimeMessageWithAttachment(session, attachment);
  GreenMailUtil.sendMimeMessage(message);

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

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

  Mail mail = mails.get(0);
  assertThat(mail.getAttachments()).hasSize(1);

  Attachment mailAttachment = mail.getAttachments().get(0);
  assertThat(mailAttachment.getFileName()).isEqualTo("attachment.txt");
  assertThat(mailAttachment.getPath()).isNull();
}
 
Example 11
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testUidSearchTextWithCharset() throws MessagingException, IOException {
    greenMail.setUser("foo2@localhost", "pwd");
    store.connect("foo2@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        final MimeMessage email = GreenMailUtil.createTextEmail("foo2@localhost", "foo@localhost",
                "some subject", "some content",
                greenMail.getSmtp().getServerSetup());

        String[][] s = {
                {"US-ASCII", "ABC", "1"},
                {"ISO-8859-15", "\u00c4\u00e4\u20AC", "2"},
                {"UTF-8", "\u00c4\u00e4\u03A0", "3"}
        };

        for (String[] charsetAndQuery : s) {
            final String charset = charsetAndQuery[0];
            final String search = charsetAndQuery[1];

            email.setSubject("subject " + search, charset);
            GreenMailUtil.sendMimeMessage(email);

            // messages[2] contains content with search text, match must be case insensitive
            final byte[] searchBytes = search.getBytes(charset);
            final Argument arg = new Argument();
            arg.writeBytes(searchBytes);
            // Try with and without quotes
            searchAndValidateWithCharset(folder, charsetAndQuery[2], charset, arg);
            searchAndValidateWithCharset(folder, charsetAndQuery[2], '"' + charset + '"', arg);
        }
    } finally {
        store.close();
    }
}
 
Example 12
Source File: Rfc822MessageTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
/**
 * Structure of test message and content type:
 * <p>
 * Message (multipart/mixed)
 * \--> MultiPart (multipart/mixed)
 * \--> MimeBodyPart (message/rfc822)
 * \--> Message (text/plain)
 */
@Test
public void testForwardWithRfc822() throws MessagingException, IOException {
    greenMail.setUser("foo@localhost", "pwd");
    final Session session = greenMail.getSmtp().createSession();

    // Message for forwarding
    Message msgToBeForwarded = GreenMailUtil.createTextEmail(
            "foo@localhost", "foo@localhost", "test newMessageWithForward", "forwarded mail content",
            greenMail.getSmtp().getServerSetup());


    // Create body part containing forwarded message
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(msgToBeForwarded, "message/rfc822");

    // Add message body part to multi part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // New main message, containing body part
    MimeMessage newMessageWithForward = new MimeMessage(session);
    newMessageWithForward.setRecipient(Message.RecipientType.TO, new InternetAddress("foo@localhost"));
    newMessageWithForward.setSubject("Fwd: " + "test");
    newMessageWithForward.setFrom(new InternetAddress("foo@localhost"));
    newMessageWithForward.setContent(multipart);   //Save changes in newMessageWithForward message
    newMessageWithForward.saveChanges();

    GreenMailUtil.sendMimeMessage(newMessageWithForward);

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);
        Message[] messages = inboxFolder.getMessages();
        MimeMessage msg = (MimeMessage) messages[0];
        assertTrue(msg.getContentType().startsWith("multipart/mixed"));
        Multipart multipartReceived = (Multipart) msg.getContent();
        assertTrue(multipartReceived.getContentType().startsWith("multipart/mixed"));
        MimeBodyPart mimeBodyPartReceived = (MimeBodyPart) multipartReceived.getBodyPart(0);
        assertTrue(mimeBodyPartReceived.getContentType().toLowerCase().startsWith("message/rfc822"));

        MimeMessage msgAttached = (MimeMessage) mimeBodyPartReceived.getContent();
        assertThat(msgAttached.getContentType().toLowerCase(), startsWith("text/plain"));
        assertArrayEquals(msgToBeForwarded.getRecipients(Message.RecipientType.TO), msgAttached.getRecipients(Message.RecipientType.TO));
        assertArrayEquals(msgToBeForwarded.getFrom(), msgAttached.getFrom());
        assertEquals(msgToBeForwarded.getSubject(), msgAttached.getSubject());
        assertEquals(msgToBeForwarded.getContent(), msgAttached.getContent());
    } finally {
        store.close();
    }
}
 
Example 13
Source File: EncodingTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
/**
 * Structure of test message and content type:
 * <p>
 * Message (multipart/alternative)
 * \--> Message (text/plain)
 */
@Test
public void testTextPlainWithUTF8() throws MessagingException, IOException {
    greenMail.setUser("foo@localhost", "pwd");
    final Session session = greenMail.getSmtp().createSession();

    MimeMultipart multipart = new MimeMultipart("alternative");

    MimeBodyPart textQP = new MimeBodyPart();
    textQP.setContent("QP Content with umlaut \u00FC", "text/javascript; charset=utf-8");
    textQP.setHeader("Content-Transfer-Encoding", "QUOTED-PRINTABLE");
    multipart.addBodyPart(textQP);

    MimeBodyPart html = new MimeBodyPart();
    html.setContent(MimeUtility.encodeText("<!doctype html>" +
            "<html lang=en>" +
            "<head>" +
            "<meta charset=utf-8>" +
            "<title>Title with Umlaut \u00FC</title>" +
            "</head>" +
            "<body>" +
            "<p>8BIT Content with umlaut ü</p>" +
            "</body>" +
            "</html>", "UTF-8", "B"), "text/html; charset=utf-8");
    html.setHeader("Content-Transfer-Encoding", "8BIT");
    multipart.addBodyPart(html);

    MimeBodyPart text = new MimeBodyPart();
    text.setText(MimeUtility.encodeText("8BIT Content with umlaut \u00FC", "UTF-8", "B"), "utf-8");
    text.setHeader("Content-Transfer-Encoding", "8BIT");
    multipart.addBodyPart(text);

    MimeBodyPart text2QP = new MimeBodyPart();
    text2QP.setText(MimeUtility.encodeText("8BIT Content with umlaut \u00FC", "UTF-8", "Q"), "utf-8");
    text2QP.setHeader("Content-Transfer-Encoding", "8BIT");
    multipart.addBodyPart(text2QP);


    // New main message, containing body part
    MimeMessage message = new MimeMessage(session);
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("foo@localhost"));
    message.setSubject("Subject ä", "UTF-8");
    message.setFrom(new InternetAddress("foo@localhost"));
    message.setContent(multipart);
    message.saveChanges();

    GreenMailUtil.sendMimeMessage(message);

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);
        Message[] messages = inboxFolder.getMessages();
        MimeMessage msg = (MimeMessage) messages[0];
        message.writeTo(new FileOutputStream(new File("t.eml")));
        assertTrue(msg.getContentType().startsWith("multipart/alternative"));
        Multipart multipartReceived = (Multipart) msg.getContent();

        assertTrue(multipartReceived.getContentType().startsWith("multipart/alternative"));

        // QP-encoded
        final BodyPart bodyPart0 = multipartReceived.getBodyPart(0);
        assertEquals("TEXT/JAVASCRIPT; charset=utf-8", bodyPart0.getContentType());
        assertEquals(textQP.getContent(), EncodingUtil.toString((InputStream) bodyPart0.getContent(), StandardCharsets.UTF_8));

        // 8-BIT-encoded
        final BodyPart bodyPart1 = multipartReceived.getBodyPart(1);
        assertEquals("TEXT/HTML; charset=utf-8", bodyPart1.getContentType());
        assertEquals(html.getContent(), bodyPart1.getContent()); // Fails

        final BodyPart bodyPart2 = multipartReceived.getBodyPart(2);
        assertEquals("TEXT/PLAIN; charset=utf-8", bodyPart2.getContentType());
        assertEquals(text.getContent(), bodyPart2.getContent());

        final BodyPart bodyPart3 = multipartReceived.getBodyPart(3);
        assertEquals("TEXT/PLAIN; charset=utf-8", bodyPart3.getContentType());
        assertEquals(text2QP.getContent(), bodyPart3.getContent());
    } finally {
        store.close();
    }
}