javax.mail.MessagingException: A3 BAD User is authenticated but not connected
Asked Answered
S

6

7

I am trying to read mail from exchange server using IMAP protocol. I have implemented the code. But following exception occurs while executing the code.This exception occurs occasionally, I didn't get the reason why this is happening.

javax.mail.MessagingException: A3 BAD User is authenticated but not connected.; nested exception is: com.sun.mail.iap.BadCommandException: A3 BAD User is authenticated but not connected. at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:958) at agent.client.attributeGroups.SendReceive.readMailAndReply(SendReceive.java:115) at agent.client.attributeGroups.MailQueue.calculateTime(MailQueue.java:45) at agent.client.MainClass$1.run(MainClass.java:72) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

code:

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

             }

Please suggest me suggestions to resolve this exception.

Splash answered 30/5, 2016 at 10:25 Comment(0)
U
5

My problem was because I use a process to move emails from inbox to another different folder. I did it in batches because move big quantities emails throw Timeout exception, so I try to move small cquantities and I avoid the Timeout exception but started to get the BadCommandException: A2 BAD User is authenticated but not connected Exception. So I looking by an amount that worked for me. I got the idea base on this post https://github.com/mscdex/node-imap/issues/689

  1. It is a Microsoft's mechanism to shut down chatty clients. The solution would be to login to this account less frequently.

  2. It is due to a bug in the IMAP implementation. If the client presents a valid user name but an invalid password, the server accepts the login, but subsequent commands fail with the aforementioned error message.

  3. It is a shared mailbox and you are using incorrect login scheme. Use Username@DomainName\SharedMailboxAlias for O365

Upthrow answered 15/3, 2019 at 20:34 Comment(0)
D
4

I have also faced that issue You didn't close the connection,it always in open.

If you use Store and Inbox it always authenticated but not connected It is authentication successfully but not connected because session is paradise.

You need to add below lines to your code.

First close the inbox after read

inbox.close(true);

Next close the store Connection

store.close();

If you close the estabilished connection correctly then it will connected according to the session.

Hope this really helpful for you

Doubleripper answered 27/9, 2016 at 5:10 Comment(0)
A
2

This error message appears to be triggered when you've connected to an Exchange mail server (such as Office365) with a valid user and the correct password, but that user doesn't have permissions for the type of connection you made.

For example, I've seen several cases now where an IMAP connection was made, the system logged in with valid credentials, and the moment the first IMAP command was sent, the server said, "User is authenticated but not connected" and closed the connection.

To correct it, we went into the Exchange server or Office365 mail settings and ensured that the IMAP permission was checked for that user.

Azaleah answered 3/6, 2022 at 22:46 Comment(0)
D
1
  • First,you should check the mail.imap.host value is equivalent to the email address's server. If you use outlook OC
  • If true,you could then try to change the protocol to IMAPS

I hope the snippets that set of properties can help many that want to recieve mails.

    sysProperties.put("mail.imap.starttls.enable", true);
    MailSSLSocketFactory sf = null;
    try {
        sf = new MailSSLSocketFactory();
    } catch (GeneralSecurityException e1) {
        e1.printStackTrace();
    }
    sf.setTrustAllHosts(true);

    sysProperties.setProperty("mail.imaps.auth.plain.disable", "true");
    sysProperties.setProperty("mail.imaps.auth.ntlm.disable", "true");

    sysProperties.put("mail.imap.ssl.socketFactory", sf);
    sysProperties.setProperty("mail.imap.port", instance.getConfigPropertiesValue(configProperties, "mail.imap.port"));
    sysProperties.setProperty("mail.imap.ssl.socketFactory.port", "993");

    Session session = Session.getDefaultInstance(sysProperties, null);

    Store store = null;
    store = session.getStore("imaps");
    store.connect("mail.serverhost", "mail.serverport"),  "mail.username,"mail.password"));
Dygall answered 3/11, 2016 at 2:25 Comment(0)
M
0

Could it be related to your exchange settings? I ran into this error with imap and outlook.office365.com. I tried to set up the same account in Outlook to make sure it was working in general. Although I was able to add the account, when I actually tried to look at the inbox Outlook became unresponsive. So in my case, it was a problem with the account in general, not with how I was trying to connect.

Maus answered 29/10, 2021 at 14:43 Comment(0)
F
-1

It appears to be a bug in the office365 server. It's telling you that your password was wrong.

Fetterlock answered 31/5, 2016 at 6:23 Comment(2)
I don't think so password is wrong, because this exception occurs occasionally. All other time it's execute fine.Splash
Try fixing common mistakes #1 and #4 and see if that helps.Fetterlock

© 2022 - 2024 — McMap. All rights reserved.