从绿色邮件服务器IMAP获取时FolderClosedException

时间:2016-04-18 07:09:02

标签: java javamail greenmail

我正在测试绿色邮件api并在尝试从绿色邮件服务器获取时收到以下错误,尽管服务器已正确启动。 我使用的是绿色邮件1.4.1,java 8,java邮件1.5.3。 下面是我一直在执行的代码以及我收到的异常。

    package greenmailtest;

    import java.io.IOException;
    import java.util.Properties;

    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Store;
    import javax.mail.URLName;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    import com.icegreen.greenmail.user.GreenMailUser;
    import com.icegreen.greenmail.user.UserException;
    import com.icegreen.greenmail.util.GreenMail;
    import com.icegreen.greenmail.util.GreenMailUtil;
    import com.icegreen.greenmail.util.ServerSetupTest;

    public class ImapIT {
        private static final String USER_PASSWORD = "abcdef123";
        private static final String USER_NAME = "hascode";
        private static final String EMAIL_USER_ADDRESS = "hascode@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;


        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 ..
            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);

            // fetch the e-mail via imap using javax.mail ..
            Properties props = new Properties();
            props.put("mail.store.protocol", "imap");
            Session session = Session.getInstance(props);

            Store store = session.getStore();
            store.connect(ServerSetupTest.IMAP.getBindAddress(), ServerSetupTest.IMAP.getPort(), user.getLogin(),
                    user.getPassword());
           // store.connect();

            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] messages = folder.getMessages();
            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());
            }
            folder.close(true);
    //        assertNotNull(messages);
    //        assertThat(1, equalTo(messages.length));
    //        assertEquals(EMAIL_SUBJECT, messages[0].getSubject());
    //        assertTrue(String.valueOf(messages[0].getContent())
    //        .contains(EMAIL_TEXT));
    //        assertEquals(EMAIL_TO, messages[0].getFrom()[0].toString());
        }

        public static void main(String[]args){
            ImapIT imap=new ImapIT();
            imap.setUp();
            try {
                imap.getMails();
                imap.tearDown();
            } catch (IOException | MessagingException | UserException
                    | InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    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.getFrom(IMAPMessage.java:321)
    at greenmailtest.ImapIT.getMails(ImapIT.java:75)
    at greenmailtest.ImapIT.main(ImapIT.java:93)

0 个答案:

没有答案