About | Examples | JBoss | FAQ | Javadocs | Blog | Download | Feedback

Testing Your Sending Code - Example 1

public void testYourSendingCode() throws Exception {
    GreenMail greenMail = new GreenMail(); //uses test ports by default
    greenMail.start();
    GreenMailUtil.sendTextEmailTest("to@localhost.com", "from@localhost.com", "subject", "body");
    assertEquals("subject", greenMail.getReceivedMessages()[0].getSubject());
    greenMail.stop();
    //That's it!
}    

Testing Your Sending Code - Example 2

This example does basically the same as the one above but is more complex as it's purpose is to demonstrate additional features and utility methods.
GreenMail greenMail;

protected void tearDown() throws Exception {
    if (null!=greenMail) {
        greenMail.stop();
    }
    super.tearDown();
}

public void testYourSendingCode() throws Exception {
    //start all email servers using non-default ports.
    //See Javadoc for details
    greenMail = new GreenMail(ServerSetupTest.ALL);
    greenMail.start();
    
    //use random content to avoid potential residual lingering problems
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    
    <PLACE YOUR SEND 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, 1));
    
    //Retrieve using GreenMail API
    Message[] messages = greenMail.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals(subject, messages[0].getSubject());
    assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());
    
    //if your send content as a 2 part multipart...
    assertTrue(messages[0].getContent() instanceof MimeMultipart);
    MimeMultipart mp = (MimeMultipart) messages[0].getContent();
    assertEquals(2, mp.getCount());
    BodyPart bp;
    assertEquals(multipart_body1, GreenMailUtil.getBody(mp.getBodyPart(0)).trim());
    assertEquals(multipart_body2, GreenMailUtil.getBody(mp.getBodyPart(1)).trim());
}    

Test Your Retrieving Code

protected void tearDown() throws Exception {
    if (null!=greenMail) {
        greenMail.stop();
    }
    super.tearDown();
}

/**
 * Places an email in a users inbox directly for testing your retrieval code
 */
public void testYourRetrievingCode_by_insertion() throws Exception {
    //start all email servers using non-default ports.
    //See Javadoc for details
    greenMail = new GreenMail(ServerSetupTest.ALL);
    greenMail.start();

    //use random content to avoid potential residual lingering problems
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    MimeMessage message = <CONSTRUCT MESSAGE >
    User user = greenMail.setUser("wael@localhost.com", "waelc", "soooosecret");
    user.deliver(message);
    assertEquals(1, greenMail.getReceivedMessages().length);

    <PLACE YOUR RETRIVE AND VERIFICATION CODE HERE>
}

/**
 * Sends an email using SMTP for testing your retrieval code
 */
public void testYourRetrievingCodeBySending() throws Exception {
    //start all email servers using non-default ports.
    //See Javadoc for details
    greenMail = new GreenMail(ServerSetupTest.ALL);
    greenMail.start();
    
    //use random content to avoid potential residual lingering problems
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest("test@localhost.com", "from@localhost.com", subject, body);
    
    //wait for max 5s for 1 email to arrive
    assertTrue(greenMail.waitForIncomingEmail(5000, 1));
    
    <PLACE YOUR RETRIVE AND VERIFICATION CODE HERE>
}