3

I have created a dummy green mail server to simulate a mailbox for my application. My application basically connects to a mail server and retrieves new mails based on some criteria. So i have created a dummy mail server for my application using green mail. Now when I start my green mail and afterwards when try to connect to the mailbox created by green mail by my original application(application under test) then the application is able to connect to it and able to read list of new mails from the green mail server but when i try to read parameters of the fetched then it says-:

javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
    at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1428)
    at com.sun.mail.imap.IMAPMessage.getSubject(IMAPMessage.java:427)
    at greenmailtest.ImapIT.getMails(ImapIT.java:79)
    at greenmailtest.ImapIT.main(ImapIT.java:94)

I am attaching my test code here:-

public  void setUp() {
        mailServer = new GreenMail(ServerSetupTest.IMAP);
        mailServer.start();
    }


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


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

        // create an e-mail message using javax.mail ..
        File file=new File("/home/sameepsinghania/Desktop/HDFC/Addcalbackfailure.png");
       // byte[] byteArray=TestMailUtil.readContentIntoByteArray(file);
        MimeMultipart multipart=TestMailUtil.createMultipartWithAttachment(EMAIL_TEXT,file, "sameep.png");

        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.setContent(multipart);
        // use greenmail to store the message
        user.deliver(message);



        Properties props = new Properties();
        Session session = Session.getInstance(props);
        URLName urlName = new URLName("imaps", "127.0.0.1",
        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);
        Message[] messages = folder.getMessages();
        for(Message message1: messages){
        System.out.println("subject "+message1.getSubject());//**this is the line where I get the exception**
}
    }


JavaMail Debug logs-:

DEBUG: setDebug: JavaMail version 1.5.3
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "127.0.0.1", port 3143, isSSL false
* OK IMAP4rev1 Server GreenMail ready
A0 CAPABILITY
* CAPABILITY IMAP4rev1 LITERAL+ QUOTA
A0 OK CAPABILITY completed.
DEBUG IMAP: protocolConnect login, host=127.0.0.1, user=hascode, password=<non-null>
DEBUG IMAP: LOGIN command trace suppressed
DEBUG IMAP: LOGIN command result: A1 OK LOGIN completed.
A2 CAPABILITY
* CAPABILITY IMAP4rev1 LITERAL+ QUOTA
A2 OK CAPABILITY completed.
DEBUG IMAP: connection available -- size: 1
A3 EXAMINE INBOX
* FLAGS (\Answered \Deleted \Draft \Flagged \Seen)
* 1 EXISTS
* 1 RECENT
* OK [UIDVALIDITY 1461648432354]
* OK [UNSEEN 1] Message 1 is the first unseen
* OK [PERMANENTFLAGS (\Answered \Deleted \Draft \Flagged \Seen)]
A3 OK [READ-ONLY] EXAMINE completed.
A4 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
    at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1428)
    at com.sun.mail.imap.IMAPMessage.getSubject(IMAPMessage.java:427)
    at greenmailtest.ImapIT.getMails(ImapIT.java:78)
sameepsi
  • 307
  • 1
  • 6
  • What does the [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) show? – Bill Shannon Apr 25 '16 at 20:48
  • @billShannon The same code works fine with greenmail 1.5 what I an using is 1.4. Why is it so? And application was able to connect to the store and retrieve message meta-data, it was when the code tried to get the actual message data the exception occurred. Anyways writing java mail debug logs also. – sameepsi Apr 26 '16 at 05:25
  • 1
    Sounds like a bug in greenmail 1.4. – Bill Shannon Apr 26 '16 at 05:33
  • @sameepsi , can you try with latest GreenMail 1.5.4? There was an issue resolved with [FETCH](https://github.com/greenmail-mail-test/greenmail/issues/187). Otherwise, please open a GreenMail issue on GitHub. – Marcel May 11 '17 at 20:41
  • Yes with latest version it is fixed. – sameepsi Aug 15 '17 at 04:34

0 Answers0