Java Code Examples for com.icegreen.greenmail.util.GreenMail#stop()

The following examples show how to use com.icegreen.greenmail.util.GreenMail#stop() . 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: ExampleReceiveNoRuleTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testReceive() throws MessagingException, IOException {
    //Start all email servers using non-default ports.
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        //Use random content to avoid potential residual lingering problems
        final String subject = GreenMailUtil.random();
        final String body = GreenMailUtil.random();
        MimeMessage message = createMimeMessage(subject, body, greenMail); // Construct message
        GreenMailUser user = greenMail.setUser("[email protected]", "waelc", "soooosecret");
        user.deliver(message);
        assertEquals(1, greenMail.getReceivedMessages().length);

        // --- Place your retrieve code here

    } finally {
        greenMail.stop();
    }
}
 
Example 2
Source File: ServerStartStopTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartStop() {
    GreenMail service = new GreenMail(ServerSetupTest.ALL);
    try {
        // Try to stop before start: Nothing happens
        service.stop();
        service.start();
        service.stop();
        // Now the server is stopped, should be started by reset command
        service.reset();
        // Start again
        service.reset();
    } finally {
        // And finally stop
        service.stop();
    }
}
 
Example 3
Source File: ConcurrentCloseIT.java    From greenmail with Apache License 2.0 6 votes vote down vote up
private void testThis() throws InterruptedException {
    final GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP);
    greenMail.setUser("test@localhost", "test@localhost");
    greenMail.start();
    final SenderThread sendThread = new SenderThread();
    try {
        sendThread.start();
        greenMail.waitForIncomingEmail(3000, 1);
        final MimeMessage[] emails = greenMail.getReceivedMessages();
        assertEquals(1, emails.length);
        sendThread.join(10000);
    } finally {
        greenMail.stop();
    }

    if (sendThread.exc != null) {
        throw sendThread.exc;
    }
}
 
Example 4
Source File: SendEmailTestCase.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * SendEmail can send an email envelope to the SMTP server.
 * @throws Exception If something goes wrong.
 */
@Test
public void sendsEmaiLToSmtpServer() throws Exception  {
    String bind = "localhost";
    int port = this.port();
    GreenMail server = this.smtpServer(bind, port);
    server.start();

    SendEmail se = new SendEmail(
        new Postman.Default(
            new SMTP(
                new Token("", "")
                    .access(new Protocol.SMTP(bind, port))
            )
        ), 
        new Envelope.MIME()
            .with(new StSender("Charles Michael <[email protected]>"))
            .with(new StRecipient("[email protected]"))
            .with(new StSubject("test subject: test email"))
            .with(new EnPlain("hello, how are you? This is a test email..."))
    );

    try {
        se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class));
        final MimeMessage[] messages = server.getReceivedMessages();
        assertTrue(messages.length == 1);
        for (final Message msg : messages) {
            assertTrue(msg.getFrom()[0].toString().contains("<[email protected]>"));
            assertTrue(msg.getSubject().contains("test email"));
        }
    } finally {
        server.stop();
    }
}
 
Example 5
Source File: SendEmailTestCase.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * SendEmail can send an email when username, password, host and port are specified.
 * @throws Exception If something goes wrong.
 */
@Test
public void sendsEmailFromGivenUser() throws Exception {
    String bind = "localhost";
    int port = this.port();
    GreenMail server = this.smtpServer(bind, port);
    server.start();
    
    System.setProperty("charles.smtp.username","mihai");
    System.setProperty("charles.smtp.password","");
    System.setProperty("charles.smtp.host","localhost");
    System.setProperty("charles.smtp.port", String.valueOf(port));

    SendEmail se = new SendEmail("[email protected]", "hello", "hello, how are you?");

    try {
        se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class));
        final MimeMessage[] messages = server.getReceivedMessages();
        assertTrue(messages.length == 1);
        for (final Message msg : messages) {
            assertTrue(msg.getFrom()[0].toString().contains("mihai"));
            assertTrue(msg.getSubject().contains("hello"));
        }
    } finally {
        server.stop();
    }
}
 
Example 6
Source File: MailPluginIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    GreenMail greenMail = new GreenMail(); // uses test ports by default
    greenMail.start();
    try {
        transactionMarker();
    } finally {
        greenMail.stop();
    }
}
 
Example 7
Source File: ExampleSendNoRuleAdvTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend() throws MessagingException, IOException {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        //Use random content to avoid potential residual lingering problems
        final String subject = GreenMailUtil.random();
        final String body = GreenMailUtil.random();

        sendTestMails(subject, body); // Place your sending code here

        //wait for max 5s for 1 email to arrive
        //waitForIncomingEmail() is useful if you're sending stuff asynchronously in a separate thread
        assertTrue(greenMail.waitForIncomingEmail(5000, 2));

        //Retrieve using GreenMail API
        Message[] messages = greenMail.getReceivedMessages();
        assertEquals(2, messages.length);

        // Simple message
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());

        //if you send content as a 2 part multipart...
        assertTrue(messages[1].getContent() instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) messages[1].getContent();
        assertEquals(2, mp.getCount());
        assertEquals("body1", GreenMailUtil.getBody(mp.getBodyPart(0)).trim());
        assertEquals("body2", GreenMailUtil.getBody(mp.getBodyPart(1)).trim());
    } finally {
        greenMail.stop();
    }
}
 
Example 8
Source File: ExampleSendNoRuleSimpleTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend() throws MessagingException {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP); //uses test ports by default
    try {
        greenMail.start();
        GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "some subject",
                "some body"); //replace this with your test message content
        assertEquals("some body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
    } finally {
        greenMail.stop();
    }
}
 
Example 9
Source File: GreenMailConfigurationTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsersAccessible() {
    GreenMail greenMail = new GreenMail(ServerSetupTest.IMAP).withConfiguration(
            testUsersAccessibleConfig()
    );
    greenMail.start();
    try {
        GreenMailConfigurationTestBase.testUsersAccessible(greenMail);
    } finally {
        greenMail.stop();
    }
}
 
Example 10
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 11
Source File: GreenMailUtilTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetAndGetQuota() throws MessagingException {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        final GreenMailUser user = greenMail.setUser("foo@localhost", "pwd");

        Store store = greenMail.getImap().createStore();
        store.connect("foo@localhost", "pwd");

        Quota testQuota = new Quota("INBOX");
        testQuota.setResourceLimit("STORAGE", 1024L * 42L);
        testQuota.setResourceLimit("MESSAGES", 5L);

        assertEquals(0, GreenMailUtil.getQuota(user, testQuota.quotaRoot).length);
        GreenMailUtil.setQuota(user, testQuota);

        final Quota[] quota = GreenMailUtil.getQuota(user, testQuota.quotaRoot);
        assertEquals(1, quota.length);
        assertEquals(2, quota[0].resources.length);

        store.close();
    } finally {
        greenMail.stop();
    }
}
 
Example 12
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();
    }
}