javax.mail.AuthenticationFailedException:AUTHENTICATE失败。 IMAP商店没有连接

时间:2018-04-24 07:39:26

标签: javamail imap

我无法连接到IMAP商店。它现在工作得更早,现在抛出错误

javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:474)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:176)

代码:

Properties properties = new Properties();
properties.put("mail.imap.host", "outlook.office365.com"));
properties.put("mail.imap.port", "993");
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.imap.socketFactory.fallback", "false");
properties.setProperty("mail.imap.socketFactory.port", String.valueOf("993")));
session = Session.getInstance(properties);
store = session.get("imap");
store.connect(username,password);

我检查了所有的链接,我尝试了一些建议的解决方案,但他们的工作。

调试信息。

  DEBUG: JavaMail version 1.4ea
    DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_112\jre\lib\javamail.providers (The system cannot find the file specified)
    DEBUG: !anyLoaded
    DEBUG: not loading resource: /META-INF/javamail.providers
    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,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
    DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
    DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
    DEBUG: !anyLoaded
    DEBUG: not loading resource: /META-INF/javamail.address.map
    DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_112\jre\lib\javamail.address.map (The system cannot find the file specified)
    DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
    DEBUG: mail.imap.fetchsize: 16384
    * OK The Microsoft Exchange IMAP4 service is ready. [TQBBADEAUABSADAAMQAwADEAQwBBADAAMAA2ADcALgBJAE4ARABQAFIARAAwADEALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
    A0 CAPABILITY
    * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
    A0 OK CAPABILITY completed.
    IMAP DEBUG: AUTH: PLAIN
    IMAP DEBUG: AUTH: XOAUTH2
    DEBUG: protocolConnect login, host=outlook.office365.com, user=pavkumar0@publicisgroupe.net, password=<non-null>
    A1 AUTHENTICATE PLAIN
    + 
    cGF2a3VtYXIwQHB1YmxpY2lzZ3JvdXBlLm5ldABwYXZrdW1hcjBAcHVibGljaXNncm91cGUubmV0AFNyaWRoYWJ2cmsrMTM=
    A1 NO AUTHENTICATE failed.
    javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
        at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:474)
        at javax.mail.Service.connect(Service.java:275)
        at javax.mail.Service.connect(Service.java:156)

4 个答案:

答案 0 :(得分:2)

身份验证失败错误即将发生,因为防火墙未启用该帐户的Imap。

答案 1 :(得分:1)

创建应用程序密码并使用它,而不是提供普通密码。

登录到Office 365的步骤获得了MYAccount->安全和隐私->其他安全验证->创建和管理应用密码

仅使用以下属性

Properties props = new Properties();
props.put("mail.imap.host", "outlook.office365.com");
props.setProperty("mail.imap.starttls.enable", "true");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.debug", "true");

答案 2 :(得分:1)

从最近升级的Outlook Office365帐户读取电子邮件时,我也遇到了这个问题。

我将代码更改为使用microsoft ews-java-api,并已解决问题。

使用microsoft ews-java-api建立连接并阅读带有附件和已通过主题的电子邮件的示例代码:

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.enumeration.search.LogicalOperator;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.core.service.schema.ItemSchema;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.FileAttachment;
import microsoft.exchange.webservices.data.search.ItemView;

import microsoft.exchange.webservices.data.core.service.folder.Folder;
import microsoft.exchange.webservices.data.search.filter.SearchFilter;
import org.springframework.util.CollectionUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class ExchangeEmailTest {
    public static void main(String[] args) {
        try {
            ExchangeService service = createConnection();
            readAttachmentEmail(service);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static ExchangeService createConnection() throws Exception {
        String email = "your email";
        String password= "your email password";
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        ExchangeCredentials credentials = new WebCredentials(email, password);
        service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
        service.setCredentials(credentials);
        return service;
    }

    private static void readAttachmentEmail(ExchangeService service) throws Exception {
        // Bind to the Inbox.
        Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
        // set number of items you want to retrieve
        ItemView view = new ItemView(1);
        List<SearchFilter> searchFilterCollection = new ArrayList<>();
        // flag to pick only email which contains attachments
        searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
        searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,"subject filter"));
        SearchFilter finalSearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
        ArrayList<Item> items =  service.findItems(inbox.getId(), finalSearchFilter, view).getItems();
        if(!CollectionUtils.isEmpty(items)) {
            Item item= items.get(0);
            System.out.println("id==========" + item.getDateTimeReceived());
            System.out.println("sub==========" + item.getSubject());
            EmailMessage message = EmailMessage.bind(service, item.getId(), new PropertySet(ItemSchema.Attachments));
            FileAttachment attachment = (FileAttachment) message.getAttachments().getItems().get(0);
            attachment.load(" < attachment save directory > "+attachment.getName());
        }

    }
}

答案 3 :(得分:0)

当我尝试在不同设备上运行代码时,我遇到了相同的 gmail 帐户问题。这是因为谷歌不允许 gmail 帐户登录到其他设备。解决方案是您应该允许访问该帐户使用以下网址:

https://accounts.google.com/DisplayUnlockCaptcha