Javamail API javax.mail.AuthenticationFailedException:读取收件箱时重置连接

时间:2016-12-27 05:13:18

标签: java javamail

我编写了以下java代码来阅读收件箱消息:

let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
        tbc.selectedIndex = 1

        // Suppose your viewcontroller is at tab bar first index.
        let transportJourneyViewControllerOBJ = tbc.viewControllers?[0] as! ViewController
        transportJourneyViewControllerOBJ.strValue = "Testvalue"
        self.present(tbc, animated: true, completion: nil)

我在行

上收到AuthenticationFailed Exception
public void readEMail() {
    try {
        String user = "<<MY_EMAIL_ADDRESS>>";
        String password = "<<MY_PASSWORD>>";

        Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties);

        Store store = session.getStore("pop3");
        store.connect(MY_HOST_NAME, user, password);

        Folder folder = store.getFolder("inbox");
        if (!folder.exists()) {
            System.out.println("inbox not found");
            System.exit(0);
        }
        folder.open(Folder.READ);

        Message[] msg = folder.getMessages();
        for (int i = 0; i < msg.length; i++) {
            String from = InternetAddress.toString(msg[i].getFrom());
            if (from != null) {
                System.out.println("From: " + from);
            }
            String replyTo = InternetAddress.toString(msg[i].getReplyTo());
            if (replyTo != null) {
                System.out.println("Reply-to: " + replyTo);
            }
            String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
            if (to != null) {
                System.out.println("To: " + to);
            }
            String subject = msg[i].getSubject();
            if (subject != null) {
                System.out.println("Subject: " + subject);
            }
            Date sent = msg[i].getSentDate();
            if (sent != null) {
                System.out.println("Sent: " + sent);
            }
            System.out.println("Message : ");
            System.out.println(msg[i].getContent());
        }
        folder.close(true);
        store.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

如果我将我的提供者从pop3更改为imap,我会得到异常store.connect(MY_HOST_NAME,user,password);

修改

使用IMAP并将debug设置为true时,将在控制台中打印以下内容:

javax.mail.AuthenticationFailedException: AUTHENTICATE failed

使用POP3时,输出为:

DEBUG: JavaMail version 1.5.6
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.5.6
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 "blrkeccas.ad.infosys.com", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN STARTTLS UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=<<HOST_NAME>>, user=<<USER_NAME>>, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 NO AUTHENTICATE failed.

0 个答案:

没有答案