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

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#createTextEmail() . 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: UserManagerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteUserShouldDeleteMail() throws Exception {
    ImapHostManager imapHostManager = new ImapHostManagerImpl(new InMemoryStore());
    UserManager userManager = new UserManager(imapHostManager);

    GreenMailUser user = userManager.createUser("[email protected]", "foo", "pwd");
    assertEquals(1, userManager.listUser().size());

    imapHostManager.createPrivateMailAccount(user);
    MailFolder otherfolder = imapHostManager.createMailbox(user, "otherfolder");
    MailFolder inbox = imapHostManager.getFolder(user, ImapConstants.INBOX_NAME);

    ServerSetup ss = ServerSetupTest.IMAP;
    MimeMessage m1 = GreenMailUtil.createTextEmail("there@localhost", "here@localhost", "sub1", "msg1", ss);
    MimeMessage m2 = GreenMailUtil.createTextEmail("there@localhost", "here@localhost", "sub1", "msg1", ss);

    inbox.store(m1);
    otherfolder.store(m2);
 
    userManager.deleteUser(user);
    assertTrue(imapHostManager.getAllMessages().isEmpty());
}
 
Example 2
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 3
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    final MimeMessage message = GreenMailUtil.createTextEmail("test@localhost", "from@localhost",
            subject, body, greenMail.getSmtp().getServerSetup());
    Transport.send(message);
    try {
        Transport.send(message, "foo", "bar");
    } catch (AuthenticationFailedException ex) {
        assertTrue(ex.getMessage().contains(AuthCommand.AUTH_CREDENTIALS_INVALID));
    }
    greenMail.setUser("foo", "bar");
    Transport.send(message, "foo", "bar");

    greenMail.waitForIncomingEmail(1500, 3);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(2, emails.length);
    for (MimeMessage receivedMsg : emails) {
        assertEquals(subject, receivedMsg.getSubject());
        assertEquals(body, GreenMailUtil.getBody(receivedMsg).trim());
    }
}
 
Example 4
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 5
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testUidSearchAll() 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());


        final IMAPFolder.ProtocolCommand uid_search_all = new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH ALL", null);
            }
        };

        // Search empty
        Response[] ret = (Response[]) folder.doCommand(uid_search_all);
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("* SEARCH", response.toString());
    } finally {
        store.close();
    }
}
 
Example 6
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private MimeMessage createTextMessage(String to, String from, String subject, String body) {
    return GreenMailUtil.createTextEmail(to, from, subject, body, server.getServerSetup());
}
 
Example 7
Source File: ExampleReceiveNoRuleTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
private MimeMessage createMimeMessage(String subject, String body, GreenMail greenMail) {
    return GreenMailUtil.createTextEmail("[email protected]", "[email protected]", subject, body, greenMail.getImap().getServerSetup());
}
 
Example 8
Source File: ExampleReceiveTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
private MimeMessage createMimeMessage() {
    return GreenMailUtil.createTextEmail("[email protected]", "[email protected]", "subject", "body", greenMail.getImap().getServerSetup());
}
 
Example 9
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();
    }
}