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

    Problem retrieving header using Java mail using IMAP

    user4484410

      Hi

      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;
              try
              {
                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);
               
                System.out.println("Connected...");

                //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


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

                   Message msg =  messages[i];
                  //System.out.println(msg.getMessageNumber());
                  //Object String;
                  //System.out.println(folder.getUID(msg)

                  subject = msg.getSubject();
                  if(/*!msg.getFlags().contains(Flags.Flag.SEEN) &&*/"Test email for UAT".equalsIgnoreCase(subject)){
                      System.out.println("*****************************************************************************");
                      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(msg.getFlags());
                  System.out.println("Body: \n"+ msg.getContent());
                  System.out.println(msg.getContentType());
                 
                  //get all headers
                  Enumeration allHeaders = msg.getAllHeaders();
                
                  while(allHeaders.hasMoreElements()){
                   javax.mail.Header header = (javax.mail.Header)allHeaders.nextElement();
                   System.out.println(header.getName() + " = "+header.getValue());
                  }
                  }/*else{
                   System.out.println("Encountered read message");
                  }*/

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

          }

       

      }