jakarta-mail - javax.mail.MessagingException : A3 BAD User is authenticated but not connected

标签 jakarta-mail exchange-server office365

我正在尝试使用 IMAP 协议(protocol)从交换服务器读取邮件。我已经实现了代码。但是在执行代码时会发生以下异常。此异常偶尔会发生,我没有得到发生这种情况的原因。

javax.mail.MessagingException:A3 BAD 用户已通过身份验证但未连接。;
嵌套异常是:
com.sun.mail.iap.BadCommandException:A3 BAD 用户已通过身份验证但未连接。
在 com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:958)
在 agent.client.attributeGroups.SendReceive.readMailAndReply(SendReceive.java:115)
在 agent.client.attributeGroups.MailQueue.calculateTime(MailQueue.java:45)
在 agent.client.MainClass$1.run(MainClass.java:72)
在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
在 java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
在 java.lang.Thread.run(Thread.java:745)

代码:

Properties props = new Properties();
            props.put("mail.imap.auth", "true");
            props.put("mail.imap.ssl.enable", "true");
            props.put("mail.imap.host", "outlook.office365.com");
            props.put("mail.imap.port", "993");
            props.put("mail.transport.protocol", "imap");  
            props.put("mail.imap.auth.plain.disable", true);
            props.put("mail.imap.auth.ntlm.disable", true);
            props.put("mail.imap.auth.gssapi.disable", true);

          //Get session object by passing credentials.
            Session session = Session.getDefaultInstance(props, 
                    new javax.mail.Authenticator(){
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(
                                    mailId, mailPass);}});

         //Creating IMAP store and connecting it to Mailbox using credentials.   
         store = session.getStore("imap");
         store.connect(mailId, mailPass);

             //Getting mailbox mails.
             inbox = store.getFolder("INBOX");
             inbox.open(Folder.READ_WRITE); 
             int totalMailCount = inbox.getMessageCount();

             //Reading all mails to Message array.
             Message[] messages = inbox.getMessages(1, totalMailCount);
             for (Message mail : messages) 
             {  
                    Address[] fromAddresses = mail.getFrom();                      
                    String mailFrom = fromAddresses[0].toString();
                    String mailSubject = mail.getSubject();
                System.out.println(mailFrom);
                    System.out.println(mailSubject);
                    mail.setFlag(Flags.Flag.DELETED,true);

                   }                       
                }

             }

请向我提出解决此异常的建议。

最佳答案

我也遇到过这个问题
您没有关闭连接,它始终处于打开状态。

如果您使用商店和收件箱,它总是经过身份验证但未连接
认证成功但未连接,因为 session 是天堂。

您需要在代码中添加以下行。

阅读后先关闭收件箱

收件箱.close(true);

接下来关闭商店连接

商店.close();

如果您正确关闭已建立的连接,那么它将根据 session 进行连接。

希望这对你真的有帮助

关于jakarta-mail - javax.mail.MessagingException : A3 BAD User is authenticated but not connected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523320/

相关文章:

c# - EWS 中 ItemView 的最大尺寸是多少?

regex - 使用 Exchange Web 服务进行正则表达式信用卡搜索?

exchangewebservices - 带有 EWS API 的 SAML

ssl - 使用 Java Mail API 通过 IMAP 协议(protocol)安全访问电子邮件地址

java - 如何从java中的.eml文件获取内部主题

java - com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口 : localhost, 25;超时-1;

exchange-server - 当我尝试通过 EWS 托管 API 读取/更新任务的 .Body 时出错 - "You must load or assign this property before you can read its value."

azure - OneDrive for Business API 发现和授权问题

c# - O365 使用Mailkit 删除邮件?

Android JavaMail 应用程序 - CertPathValidatorException : Trust anchor for certification path not found