javaemailimapgreenmail

Can't access properties of email received by GreenMail


I'm trying to use GreenMail as a mockup mail server for unit testing. It's job is to await a IMAP/POP3 request and deliver a dummy mail, but I can't get it to work.

public class EMailMonitoringTest {
private static final String USER_PASSWORD = "test";
private static final String USER_NAME = "jDownloaderTestUser";
private static final String EMAIL_USER_ADDRESS = "jdtest@localhost";
private static final String EMAIL_TO = "someone@localhost.com";
private static final String EMAIL_SUBJECT = "Test E-Mail";
private static final String EMAIL_TEXT = "This is a test e-mail.";
private static final String LOCALHOST = "127.0.0.1";
private GreenMail mailServer;

@Before
public void setUp() {
    mailServer = new GreenMail(ServerSetupTest.ALL);
    mailServer.start();
}

@After
public void tearDown() {
    mailServer.stop();
}

@Test
public void getMails() throws AddressException, MessagingException, FolderException, UserException, IOException {
    // create user on mail server
    GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);

    // create an e-mail message using javax.mail ..
    MimeMessage message = new MimeMessage((Session) null);
    message.setFrom(new InternetAddress(EMAIL_TO));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_USER_ADDRESS));
    message.setSubject(EMAIL_SUBJECT);
    message.setText(EMAIL_TEXT);

    // use greenmail to store the message
    user.deliver(message);

    // check received messages
    Message[] messages = mailServer.getReceivedMessages();
    for (Message m : messages) {
        System.out.println("*** Class: " + m.getClass() + " ***");
        System.out.println("From: " + m.getFrom()[0]);
        System.out.println("To: " + m.getRecipients(Message.RecipientType.TO)[0]);
        System.out.println("Subject: " + m.getSubject());
        System.out.println("Content: " + m.getContent());
    }

No problem here, the output so far is what it look like:

** Class: class javax.mail.internet.MimeMessage **

From: someone@localhost.com

To: jdtest@localhost

Subject: Test E-Mail

Content: This is a test e-mail.

After that, I try to get the mail from the GreenMail IMAP server:

    // fetch the e-mail via imap using javax.mail ..
    Properties props = new Properties();
    Session session = Session.getInstance(props);
    URLName urlName = new URLName("imap", LOCALHOST,
    ServerSetupTest.IMAP.getPort(), null, user.getLogin(),
    user.getPassword());
    Store store = session.getStore(urlName);
    store.connect();

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);
    messages = folder.getMessages();
    System.out.println("Size of Messages: " + messages.length);
    for (Message m : messages) {
        System.out.println("** Class: " + m.getClass() + " **");
        System.out.println("From: " + m.getFrom()[0].toString());
        System.out.println("To: " + m.getRecipients(Message.RecipientType.TO)[0].toString());
        System.out.println("Subject: " + m.getSubject().toString());
        System.out.println("Content: " + m.getContent().toString());
    }

and my reward for this is a

Size of Messages: 1

** Class: class com.sun.mail.imap.IMAPMessage **

and a FolderClosedException:

javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1217) at com.sun.mail.imap.IMAPMessage.getFrom(IMAPMessage.java:252) at jd.controlling.EMailMonitoringTest.test(EMailMonitoringTest.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Trying to access folder.getMessage(0) just results in a IndexOutOfBoundsException, but there IS one Message of class IMAPMessage stored in the Array. Someone knows about this?

Ah yes, I tried the examples on hashCode, but they also fail with the same reason... FolderClosedException when accessing the properties of the received IMAP message...

Gratefully would accept any hints.


Solution

  • When using IMAP, you configure LOCALHOST but never told GreenMail to use LOCALHOST when you startup/create GreenMail.

    Use the binding address of the GreenMail server ServerSetupTest.IMAP.getBindAddress() instead of LOCALHOST:

    // fetch the e-mail via imap using javax.mail ..
    // Hint: Create session via GreenMailUtil
    Session session = GreenMailUtil.getSession(ServerSetupTest.IMAP);
    // Use configured host address instead of supplying a 
    URLName urlName = new URLName("imap", ServerSetupTest.IMAP.getBindAddress(),
       ServerSetupTest.IMAP.getPort(), null, user.getLogin(),
       user.getPassword());
    Store store = session.getStore(urlName);
    store.connect();`