1 Reply Latest reply: May 13, 2013 2:03 AM by Bill Shannon-Oracle RSS

    getting messagingException

    Prem
      Hi,

      I am writing a simple client program to read mail using IMAP protocol but , I am getting following exception. I did search on net but didn't find any solution . Please help me out.
      javax.mail.MessagingException: A4 NO There is no replica for that mailbox on this server.;
        nested exception is:
           com.sun.mail.iap.CommandFailedException: A4 NO There is no replica for that mailbox on this server.
           at com.sun.mail.imap.IMAPFolder.doCommand(IMAPFolder.java:2873)
           at com.sun.mail.imap.IMAPFolder.exists(IMAPFolder.java:514)
           at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:382)
           at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:934)
           at readEmails.processMail(readEmails.java:60)
           at readEmails.<init>(readEmails.java:20)
           at readEmails.main(readEmails.java:166)
      Caused by: com.sun.mail.iap.CommandFailedException: A4 NO There is no replica for that mailbox on this server.
           at com.sun.mail.iap.Protocol.handleResult(Protocol.java:351)
           at com.sun.mail.imap.protocol.IMAPProtocol.doList(IMAPProtocol.java:1100)
           at com.sun.mail.imap.protocol.IMAPProtocol.list(IMAPProtocol.java:1046)
           at com.sun.mail.imap.IMAPFolder$1.doCommand(IMAPFolder.java:516)
           at com.sun.mail.imap.IMAPFolder.doProtocolCommand(IMAPFolder.java:2918)
           at com.sun.mail.imap.IMAPFolder.doCommand(IMAPFolder.java:2868)
           ... 6 more
      following is the code :
           import javax.mail.AuthenticationFailedException;
           import javax.mail.Folder;
           import javax.mail.FolderClosedException;
           import javax.mail.FolderNotFoundException;
           import javax.mail.Message;
           import javax.mail.Multipart;
           import javax.mail.NoSuchProviderException;
           import javax.mail.Part;
           import javax.mail.ReadOnlyFolderException;
           import javax.mail.Session;
           import javax.mail.Store;
           import javax.mail.StoreClosedException;
           import javax.mail.internet.InternetAddress;
            
           public class readEmails {
            
           //Constructor Call
           public readEmails() {
              processMail();
           }
            
           //Responsible for printing Data to Console
           private void printData(String data) {
              System.out.println(data);
           }
            
           public void processMail() {
              Session session = null;
              Store store = null;
              Folder folder = null;
              Message message = null;
              Message[] messages = null;
              Object messagecontentObject = null;
              String sender = null;
              String subject = null;
              Multipart multipart = null;
              Part part = null;
              String contentType = null;
            
              try {
                 printData("--------------processing mails started-----------------");
                 session = Session.getDefaultInstance(System.getProperties(), null);
            
                 printData("getting the session for accessing email.");
                 store = session.getStore("imap");
            //     store.connect("hostname","username","password");
                 store.connect("email.*******","***","****");
                 printData("Connection established with IMAP server.");
            
                 // Get a handle on the default folder
                 folder = store.getDefaultFolder();
            
                 printData("Getting the Inbox folder.");
            
                 // Retrieve the "Inbox"
                 folder = folder.getFolder("Inbox");
            
                 //Reading the Email Index in Read / Write Mode
                 folder.open(Folder.READ_ONLY);
            
                 // Retrieve the messages
                 messages = folder.getMessages();
            
                 // Loop over all of the messages
                 for (int messageNumber = 0; messageNumber <2; messageNumber++) {
                      // Retrieve the next message to be read
                   message = messages[messageNumber];
            
                      // Retrieve the message content
                      messagecontentObject = message.getContent();
            
                      // Determine email type
                      if (messagecontentObject instanceof Multipart) {
                          printData("Found Email with Attachment");
                          sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
            
                          // If the "personal" information has no entry, check the address for the sender information
                          printData("If the personal information has no entry, check the address for the sender information.");
            
                       if (sender == null) {
                         sender = ((InternetAddress) message.getFrom()[0]).getAddress();
                        printData("sender in NULL. Printing Address:" + sender);
                       }
                          printData("Sender -." + sender);
            
                          // Get the subject information
                          subject = message.getSubject();
            
                          printData("subject=" + subject);
            
                          // Retrieve the Multipart object from the message
                          multipart = (Multipart) message.getContent();
            
                          printData("Retrieve the Multipart object from the message");
            
                          // Loop over the parts of the email
                          for (int i = 0; i < multipart.getCount(); i++) {
                               // Retrieve the next part
                               part = multipart.getBodyPart(i);
            
                               // Get the content type
                               contentType = part.getContentType();
            
                              // Display the content type
                        printData("Content: " + contentType);
            
                              if (contentType.startsWith("text/plain")) {
                          printData("---------reading content type text/plain  mail -------------");
                        } else {
                          // Retrieve the file name
                          String fileName = part.getFileName();
                          printData("retrive the fileName="+ fileName);
                        }
                      }
                   } else {
                      printData("Found Mail Without Attachment");
                      sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
            
                         // If the "personal" information has no entry, check the address for the sender information
                      printData("If the personal information has no entry, check the address for the sender information.");
            
                         if (sender == null) {
                     sender = ((InternetAddress) message.getFrom()[0]).getAddress();
                     printData("sender in NULL. Printing Address:" + sender);
                     }
            
                        // Get the subject information
                     subject = message.getSubject();
                     printData("subject=" + subject);
                 }
                 }
            
                 // Close the folder
                 folder.close(true);
            
                 // Close the message store
                 store.close();
             } catch(AuthenticationFailedException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             } catch(FolderClosedException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             } catch(FolderNotFoundException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             }  catch(NoSuchProviderException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             } catch(ReadOnlyFolderException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             } catch(StoreClosedException e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             } catch (Exception e) {
                printData("Not able to process the mail reading.");
                e.printStackTrace();
             }
           }
            
           //Main  Function for The readEmail Class
           public static void main(String args[]) {
               //Creating new readEmail Object
               readEmails readMail = new readEmails();
            
               //Calling processMail Function to read from IMAP Account
               //readMail.processMail();
           }
            
           }
      Edited by: Prem on May 12, 2013 2:43 PM
        • 1. Re: getting messagingException
          Bill Shannon-Oracle
          Are you using Microsoft Exchange?

          I think you're running into "IMAP referrals" - a feature where the IMAP server can tell the client to
          connect to a different server where the mailbox is really located. If you turn on JavaMail session
          debugging and examine the protocol trace you might see the information about the referral.
          Then you should be able to connect directly to the server it's referring you to.