3 Replies Latest reply: Nov 28, 2013 5:11 PM by Bshannon-Oracle RSS

    Problem retrieving header using Java mail using IMAP



      We are faced with a problem while retreiving the exact "To" header using java mail while polling on a Microsoft Exchange server mailbox. Details are provided below


      1. As per project requirement, we have created a single mailbox and added multiple(unique) email aliases(say 123@def.com and 435@pqr.com) to it. The mailbox also has a preferred email address(default) (assume ABC@XYZ.com).

      2. An email sent to any of the above mentioned aliases would landup in the same mailbox

      3. We have an email poller(written in Java Mail) which retrieves the emails from this mailbox and reads the header.

      4. The problem is, in the "To" header of the email sent to a specific alias(say 123@def.com) we were expecting the exact address(123@def.com) but instead we are getting the preferred email address(ABC@XYZ.com). This is happening for all aliases.

      5. But if we retrieve the same email using a microsoft outlook client, and check for the internet headers, we are able to see the exact alias name(123@def.com)


      We need some help in understanding whether this is a problem with Java mail, or it is some configuration on the email server that is causing this. It is very critical to get the exact email address as there are downstream business processing based on to whom the email was sent.


      Thanks for any help in advance.


      Sample code used by the poller is like below:


          public void readMail() throws MessagingException, IOException {
              IMAPFolder folder = null;
              Store store = null;
              String subject = null;
              Flag flag = null;
                Properties props = System.getProperties();
                props.setProperty("mail.store.protocol", "imaps");

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

                store = session.getStore("imaps");
                store.connect(HOST,ACCOUNT, PASSWORD);

                //folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for other email account
                folder = (IMAPFolder) store.getFolder("inbox"); //This works for both email account

                Message[] messages = folder.getMessages();
                System.out.println("No of Messages : " + folder.getMessageCount());
                System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
                for (int i=0; i < messages.length;i++)

                   Message msg =  messages[i];
                  //Object String;

                  subject = msg.getSubject();
                  if(/*!msg.getFlags().contains(Flags.Flag.SEEN) &&*/"Test email for UAT".equalsIgnoreCase(subject)){
                      System.out.println("MESSAGE " + (i + 1) + ":");
                  System.out.println("Subject: " + subject);
                  System.out.println("From: " + msg.getFrom()[0]);
                 System.out.println("To: "+msg.getAllRecipients()[0]);
                  System.out.println("Date: "+msg.getReceivedDate());
                  System.out.println("Size: "+msg.getSize());
                  System.out.println("Body: \n"+ msg.getContent());
                  //get all headers
                  Enumeration allHeaders = msg.getAllHeaders();
                   javax.mail.Header header = (javax.mail.Header)allHeaders.nextElement();
                   System.out.println(header.getName() + " = "+header.getValue());
                   System.out.println("Encountered read message");

                if (folder != null && folder.isOpen()) { folder.close(true); }
                if (store != null) { store.close(); }




        • 1. Re: Problem retrieving header using Java mail using IMAP

          The To header of the message should have the address the message was actually sent to.  I don't think there's a guarantee that that will be the zeroth element of the list of all recipients, so best to explicitly ask for the RecipientType.TO recipients.  If the To header is wrong, then Exchange is modifying the message from what the sender specified, and I don't know if there's a way to make Exchange stop doing that.

          • 2. Re: Problem retrieving header using Java mail using IMAP

            Hi bshannon


            Thanks for the answer. I still have a query. It is that we are able to view the correct alias in the "TO" header using an Outlook email client, so I don't think Exchange server is modifying the message. It is only that the email body polled by javamail is containing the modified TO address. Can you suggest if there is any jar dependency for this?


            Your help is much appreciated!

            • 3. Re: Problem retrieving header using Java mail using IMAP

              JavaMail can only show you what the server shows to JavaMail.  If you're getting the wrong information in the To header, it's because the server is giving you the wrong information.  Outlook uses a Microsoft proprietary protocol to talk to the server, so it might be getting different information.  I recommend that you report this problem to Microsoft.