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

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#sendTextEmail() . 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: ExamplePurgeAllEmailsTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testremoveAllMessagesInImapMailbox() throws FolderException {
    try (Retriever retriever = new Retriever(greenMailRule.getImap())) {
        greenMailRule.setUser("foo@localhost", "pwd");
        GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
                "Test subject", "Test message", ServerSetupTest.SMTP);
        assertTrue(greenMailRule.waitForIncomingEmail(1));
        greenMailRule.purgeEmailFromAllMailboxes();
        Message[] messages = retriever.getMessages("foo@localhost", "pwd");
        assertEquals(0, messages.length);
    }

}
 
Example 2
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeEachTest() throws NoSuchProviderException {
    user = greenMail.setUser("foo@localhost", "pwd");

    int numberOfMails = 10;
    for (int i = 0; i < numberOfMails; i++) {
        GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test search " + i,
                "Test message content" + i,
                greenMail.getSmtp().getServerSetup());
    }
    greenMail.waitForIncomingEmail(numberOfMails);

    store = greenMail.getImap().createStore();
}
 
Example 3
Source File: AllocateAvailablePortTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void ensureThatMailCanActuallyBeSentToTheAllocatedPort() {
    GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "subject", "body",
            smtpServerAtPort(greenMail.getSmtp().getPort()));

    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertThat(emails.length, is(1));
}
 
Example 4
Source File: EscapingTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapeSubject() throws MessagingException, IOException {
    String to = "to@localhost";
    String subject = "Subject?<>/|\\\\.%\\\"*?:{[]}!";
    greenMail.setUser(to, to);
    final String from = "from@localhost";
    GreenMailUtil.sendTextEmail(to, from, subject, "msg", greenMail.getSmtp().getServerSetup());
    greenMail.waitForIncomingEmail(5000, 1);

    retrieveAndCheck(greenMail.getPop3(), to, from, subject);
    retrieveAndCheck(greenMail.getImap(), to, from, subject);
}
 
Example 5
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * https://tools.ietf.org/html/rfc3501#page-37 :
 * <q>
 *     Renaming INBOX is permitted, and has special behavior.  It moves
 *     all messages in INBOX to a new mailbox with the given name,
 *     leaving INBOX empty.  If the server implementation supports
 *     inferior hierarchical names of INBOX, these are unaffected by a
 *     rename of INBOX.
 *  </q>
 *
 * @throws MessagingException
 */
@Test
public void testRenameINBOXFolder() throws MessagingException {
    greenMail.setUser("foo@localhost", "pwd");
    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test subject",
            "Test message", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {

        // Create some folders
        Folder inboxFolder = store.getFolder("INBOX");
        assertTrue(inboxFolder.exists());
        inboxFolder.open(Folder.READ_ONLY);
        assertEquals(1, inboxFolder.getMessages().length);

        Folder inboxRenamedFolder = store.getFolder("INBOX-renamed");
        assertFalse(inboxRenamedFolder.exists());

        inboxFolder.close(true);
        inboxFolder.renameTo(inboxRenamedFolder);
        assertTrue(inboxRenamedFolder.exists());
        inboxRenamedFolder.open(Folder.READ_ONLY);
        assertEquals(1, inboxRenamedFolder.getMessages().length);

        inboxFolder = store.getFolder("INBOX");
        assertTrue(inboxFolder.exists());
        inboxFolder.open(Folder.READ_ONLY);
        assertEquals(0, inboxFolder.getMessages().length);
    } finally {
        store.close();
    }
}
 
Example 6
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testFolderRequiringEscaping() throws MessagingException {
    greenMail.setUser("foo@localhost", "pwd");
    GreenMailUtil.sendTextEmail("foo@localhost", "foo@localhost", "test subject", "", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {

        // Create some folders
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_ONLY);

        final Folder folderRequiringEscaping = inboxFolder.getFolder("requires escaping Ä");
        assertTrue(folderRequiringEscaping.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES));
        folderRequiringEscaping.open(Folder.READ_WRITE);

        assertEquals(0, folderRequiringEscaping.getMessageCount());
        assertEquals(1, inboxFolder.getMessageCount());

        inboxFolder.copyMessages(inboxFolder.getMessages(), folderRequiringEscaping);

        folderRequiringEscaping.expunge(); // invalidates folder cache
        assertEquals(1, folderRequiringEscaping.getMessageCount());
    } finally {
        store.close();
    }
}
 
Example 7
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpunge() throws MessagingException {
    greenMail.setUser("foo@localhost", "pwd");

    for (int i = 0; i < 6; i++) {
        GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test subject #" + i,
                "Test message", ServerSetupTest.SMTP);
    }
    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();
        assertEquals(6, messages.length);
        inboxFolder.setFlags(new int[]{2, 3}, new Flags(DELETED), true); // 1 and 2, offset is not zero-based

        assertFalse(inboxFolder.getMessage(1).isSet(DELETED));
        assertTrue(inboxFolder.getMessage(2).isSet(DELETED));
        assertTrue(inboxFolder.getMessage(3).isSet(DELETED));
        assertFalse(inboxFolder.getMessage(4).isSet(DELETED));
        assertFalse(inboxFolder.getMessage(5).isSet(DELETED));
        assertFalse(inboxFolder.getMessage(6).isSet(DELETED));
        assertEquals(2, inboxFolder.getDeletedMessageCount());
        Message[] expunged = inboxFolder.expunge();
        assertEquals(2, expunged.length);

        messages = inboxFolder.getMessages();
        assertEquals(4, messages.length);
        assertEquals("Test subject #0", messages[0].getSubject());
        assertEquals("Test subject #3", messages[1].getSubject());
        assertEquals("Test subject #4", messages[2].getSubject());
        assertEquals("Test subject #5", messages[3].getSubject());
    } finally {
        store.close();
    }
}
 
Example 8
Source File: ConcurrentCloseIT.java    From greenmail with Apache License 2.0 5 votes vote down vote up
public void run() {
    try {
        GreenMailUtil.sendTextEmail("test@localhost", "from@localhost", "abc", "def", ServerSetupTest.SMTP);
    } catch (final Throwable e) {
        exc = new IllegalStateException(e);
    }
}
 
Example 9
Source File: GreenMailUtilTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendTextEmailTest() throws Exception {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.setUser("foo@localhost", "pwd");
        greenMail.start();
        GreenMailUtil.sendTextEmail("\"Foo, Bar\" <foo@localhost>", "\"Bar, Foo\" <bar@localhost>",
                "Test subject", "Test message", ServerSetupTest.SMTP);
        greenMail.waitForIncomingEmail(1);

        Store store = greenMail.getImap().createStore();
        store.connect("foo@localhost", "pwd");
        try {
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] msgs = folder.getMessages();
            assertTrue(null != msgs && msgs.length == 1);
            Message m = msgs[0];
            assertEquals("Test subject", m.getSubject());
            Address[] a = m.getRecipients(Message.RecipientType.TO);
            assertTrue(null != a && a.length == 1
                    && a[0].toString().equals("\"Foo, Bar\" <foo@localhost>"));
            a = m.getFrom();
            assertTrue(null != a && a.length == 1
                    && a[0].toString().equals("\"Bar, Foo\" <bar@localhost>"));
            assertTrue(m.getContentType().toLowerCase()
                    .startsWith("text/plain"));
            assertEquals("Test message", m.getContent());
        } finally {
            store.close();
        }
    } finally {
        greenMail.stop();
    }
}
 
Example 10
Source File: GreenMailStandaloneRunnerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoRun() throws MessagingException {
    GreenMailStandaloneRunner runner = new GreenMailStandaloneRunner();
    final Properties properties = new Properties();
    properties.setProperty(PropertiesBasedServerSetupBuilder.GREENMAIL_VERBOSE, "");
    properties.setProperty("greenmail.setup.test.smtp", "");
    properties.setProperty("greenmail.setup.test.imap", "");
    properties.setProperty(PropertiesBasedGreenMailConfigurationBuilder.GREENMAIL_USERS,
            "test1:pwd1,test2:pwd2@localhost");
    runner.doRun(properties);

    GreenMailUtil.sendTextEmail("test2@localhost", "test1@localhost", "Standalone test", "It worked",
            ServerSetupTest.SMTP);

    final Session session = runner.getGreenMail().getImap().createSession();
    assertTrue(session.getDebug());
    Store store = session.getStore("imap");
    try {
        store.connect("test2", "pwd2");
        final Folder folder = store.getFolder("INBOX");
        try {
            folder.open(Folder.READ_ONLY);
            Message msg = folder.getMessages()[0];
            assertEquals("test1@localhost", msg.getFrom()[0].toString());
            assertEquals("Standalone test", msg.getSubject());
        } finally {
            folder.close(true);
        }
    } finally {
        store.close();
    }
}
 
Example 11
Source File: DockerServiceIT.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllServices() throws MessagingException, InterruptedException {
    // Ugly workaround : GreenMail in docker starts with open TCP connections,
    //                   but TLS sockets might not be ready yet.
    TimeUnit.SECONDS.sleep(1);

    // Send messages via SMTP and secure SMTPS
    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
            "test1", "Test GreenMail Docker service",
            ServerSetupTest.SMTP.createCopy(bindAddress));
    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
            "test2", "Test GreenMail Docker service",
            ServerSetupTest.SMTPS.createCopy(bindAddress));

    // IMAP
    for (ServerSetup setup : Arrays.asList(
            ServerSetupTest.IMAP.createCopy(bindAddress),
            ServerSetupTest.IMAPS.createCopy(bindAddress),
            ServerSetupTest.POP3.createCopy(bindAddress),
            ServerSetupTest.POP3S.createCopy(bindAddress))) {
        final Store store = Session.getInstance(setup.configureJavaMailSessionProperties(null, false)).getStore();
        store.connect("foo@localhost", "foo@localhost");
        try {
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            assertEquals("Can not check mails using "+store.getURLName(), 2, folder.getMessageCount());
        } finally {
            store.close();
        }
    }
}
 
Example 12
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testUIDExpunge() throws MessagingException {
    greenMail.setUser("foo@localhost", "pwd");

    // Create some test emails
    int numberOfEmails = 10;
    long[] uids = new long[numberOfEmails];
    for (int i = 0; i < numberOfEmails; i++) {
        GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test UID expunge #" + i,
                "Test message", greenMail.getSmtp().getServerSetup());
    }

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);

        Message[] messages = folder.getMessages();
        assertEquals(numberOfEmails, messages.length);

        // Mark even as deleted ...
        Message[] msgsForDeletion = new Message[uids.length / 2];
        for (int i = 0; i < messages.length; i++) {
            assertFalse(messages[i].getFlags().contains(Flags.Flag.DELETED));
            uids[i] = folder.getUID(messages[i]);
            if (i % 2 == 0) { // Deleted
                messages[i].setFlag(Flags.Flag.DELETED, true);
                msgsForDeletion[i / 2] = messages[i];
            }
        }

        // ... and expunge (with UID)
        folder.expunge(msgsForDeletion);

        // Check
        for (int i = 0; i < uids.length; i++) {
            final Message message = folder.getMessageByUID(uids[i]);
            if (i % 2 == 0) { // Deleted
                assertNull(message);
            } else {
                assertFalse("" + i, message.isExpunged());
                assertFalse("" + i, message.getFlags().contains(DELETED));
            }
        }
    } finally {
        store.close();
    }
}
 
Example 13
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppend() throws MessagingException {
    greenMail.setUser("foo@localhost", "pwd");

    GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test Append",
            "Test message", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder inboxFolder = (IMAPFolder) store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);

        Message[] messages = inboxFolder.getMessages();
        assertEquals(1, messages.length);
        Message message = messages[0];

        Message[] toBeAppended = new Message[]{
                new MimeMessage((MimeMessage) message) // Copy
        };
        toBeAppended[0].setSubject("testAppend#1");

        inboxFolder.appendMessages(toBeAppended);
        messages = inboxFolder.getMessages();
        assertEquals(2, messages.length);

        // UIDPLUS
        toBeAppended[0] = new MimeMessage((MimeMessage) message);
        toBeAppended[0].setSubject("testAppend#2");

        final AppendUID[] appendUIDs = inboxFolder.appendUIDMessages(toBeAppended); // Copy again
        long uid = appendUIDs[0].uid;
        Message newMsg = inboxFolder.getMessageByUID(uid);
        assertEquals(toBeAppended[0].getSubject(), newMsg.getSubject());
        assertEquals(appendUIDs[0].uidvalidity, inboxFolder.getUIDValidity());
        messages = inboxFolder.getMessages();
        assertEquals(3, messages.length);
    } finally {
        store.close();
    }
}
 
Example 14
Source File: GreenMailBean.java    From greenmail with Apache License 2.0 3 votes vote down vote up
/**
 * Sends a mail message to the GreenMail server.
 * <p>
 * Note: SMTP or SMTPS must be configured.
 *
 * @param theTo      the <em>TO</em> field.
 * @param theFrom    the <em>FROM</em>field.
 * @param theSubject the subject.
 * @param theContent the message content.
 */
public void sendEmail(final String theTo, final String theFrom, final String theSubject,
                      final String theContent) {
    if (null == smtpServerSetup) {
        throw new IllegalStateException("Can not send mail, no SMTP or SMTPS setup found");
    }
    GreenMailUtil.sendTextEmail(theTo, theFrom, theSubject, theContent, smtpServerSetup);
}