Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:12:48+00:00 2026-06-09T20:12:48+00:00

I’ve a class which reads email using the javamail api, the problem I’m facing

  • 0

I’ve a class which reads email using the javamail api, the problem I’m facing is that when I send a mail containing a .msg attachement from gmail,my code can read it easily,but when I send the same mail for outlook client ,the “.msg” attachement does not show up

the code for my class is as follows:

public class TestEmail {
    static int count = 5;
    static String ipAddress[] = Utility.getipAddress();
    static String auth[] = Utility.getEmailPasswd();

    private static final String SMTP_AUTH_USER = auth[0];
    private static final String SMTP_AUTH_PWD  = auth[1];

    private static String defaultServer=ipAddress[1];
    private static String domainServer=ipAddress[0];

    public static void main(String[] args) throws Exception {
        /*************** Variable Declaration *********************/

        System.out.println("SMTP_AUTH_USER <><> " +  SMTP_AUTH_USER);
        System.out.println("SMTP_AUTH_PWD <><> " +  SMTP_AUTH_PWD);
        System.out.println("defaultServer <><> " +  defaultServer);
        System.out.println("domainServer <><> " +  domainServer);

        int noOfEmail = 0;
        String from = "", replyTo = "", to = "", subject = "", cc = "", bcc = "", msgId = "";
        Date sentDate = new Date();
        String fileName = "";

        /************** End of Variable Declaration ****************/
        Properties props = System.getProperties();
        props.put("mail.smtp.submitter", new MailAuthenticator());
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", domainServer);
        props.put("mail.smtp.port", "25");
        props.put("mail.imap.port", "143");
        props.put("mail.smtp.localhost", defaultServer);
//      props.put("mail.debug", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getDefaultInstance(props,new MailAuthenticator());
//      Store store = session.getStore("pop3");
        Store store = session.getStore("imap");
        store.connect(domainServer, null, null);
//      session.setDebug(true);

        /************ Reading Inbox *********/
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        /************ No of Email *********/
        noOfEmail = inbox.getMessageCount();
        System.out.println("No Of Mail <><> " + noOfEmail);

        /******** Search Unread Messase ********/
        FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        Message messages[] = inbox.search(ft);
        System.out.println("Total UNREAD MESSAGE <><> " +  messages.length);

        /*********** Traverse The Messase *******/
        for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                String contentType = message.getContentType ();
                String content = Utility.splitContentType(contentType);
                boolean multiPart = false;
                if (content.equalsIgnoreCase("mixed")) {
                    multiPart = true;
                } else {
                    multiPart = false;
                }
                if (multiPart == true) {
                    Multipart mp = (Multipart) message.getContent();
                    for (int j = 0, n = mp.getCount(); j < n; j++) {
//                      System.out.println("n Valuev <<><> " + n);
                        Part part = mp.getBodyPart(j);
                        BodyPart bodyPart = mp.getBodyPart(j);
                        String disposition = part.getDisposition();
//                      System.out.println("disposition <><><><> " + disposition);

                        if ((disposition != null)&& ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
                            System.out.println("INSIDE IF ATTACHMENT");
                            fileName = bodyPart.getFileName();
                            System.out.println("fileName <><> " + fileName);
                            String ext = Utility.splitFileName(fileName);
                            if(ext.equalsIgnoreCase("msg")){
                                System.out.println("INSIDE MSG");
                                saveFile(fileName, part.getInputStream());
//                              File emlFile = new File("C:/test/" + fileName); // For Windows
                                File emlFile = new File("/usr/AirtelMail/" + fileName);
                                InputStream source = new FileInputStream(emlFile);
                                MimeMessage message1 = new MimeMessage(session, source);
                                from = InternetAddress.toString(message1.getFrom());
                                replyTo = InternetAddress.toString(message1
                                        .getReplyTo());
                                to = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.TO));
                                cc = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.CC));
                                bcc = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.BCC));
                                subject = message1.getSubject();
                                sentDate = message1.getSentDate();
                                msgId = message1.getMessageID();
                                if (from != null && replyTo != null && to != null
                                        && subject != null && sentDate != null) {
                                    System.out.println("From: " + from);
                                    System.out.println("Reply-to: " + replyTo);
                                    System.out.println("To: " + to);
                                    System.out.println("Subject: " + subject);
                                    System.out.println("Sent Date: " + sentDate);
                                }
                            }else if(ext.equalsIgnoreCase("eml")){
                                System.out.println("INSIDE EML");
                                saveFile(fileName, part.getInputStream());
//                              File emlFile = new File("C:/test/" + fileName); // For Windows
                                File emlFile = new File("/usr/AirtelMail/" + fileName);
                                InputStream source = new FileInputStream(emlFile);
                                MimeMessage message1 = new MimeMessage(session, source);
                                from = InternetAddress.toString(message1.getFrom());
                                replyTo = InternetAddress.toString(message1
                                        .getReplyTo());
                                to = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.TO));
                                cc = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.CC));
                                bcc = InternetAddress.toString(message1
                                        .getRecipients(Message.RecipientType.BCC));
                                subject = message1.getSubject();
                                sentDate = message1.getSentDate();
                                msgId = message1.getMessageID();
                                if (from != null && replyTo != null && to != null
                                        && subject != null && sentDate != null) {
//                                  System.out.println("From: " + from);
//                                  System.out.println("Reply-to: " + replyTo);
//                                  System.out.println("To: " + to);
//                                  System.out.println("Subject: " + subject);
//                                  System.out.println("Sent Date: " + sentDate);
                                }
                            }else{
                                System.out.println("INSIDE NOT EML");
                                from = InternetAddress.toString(message.getFrom());
                                replyTo = InternetAddress.toString(message.getReplyTo());
                                to = InternetAddress.toString(message
                                        .getRecipients(Message.RecipientType.TO));
                                cc = InternetAddress.toString(message
                                        .getRecipients(Message.RecipientType.CC));
                                bcc = InternetAddress.toString(message
                                        .getRecipients(Message.RecipientType.BCC));
                                subject = message.getSubject();
                                sentDate = message.getSentDate();
                                msgId = "";

                                if (from != null && replyTo != null && to != null
                                        && subject != null && sentDate != null) {
//                                  System.out.println("From: " + from);
//                                  System.out.println("Reply-to: " + replyTo);
//                                  System.out.println("To: " + to);
//                                  System.out.println("Subject: " + subject);
//                                  System.out.println("Sent Date: " + sentDate);
                                }
                            }// End Else
                        }// End Check disposition
                    } // End of j loop
                } else {
                    System.out.println("INSIDE IF NOT ATTACHMENT");
                    from = InternetAddress.toString(message.getFrom());
                    replyTo = InternetAddress.toString(message.getReplyTo());
                    to = InternetAddress.toString(message
                            .getRecipients(Message.RecipientType.TO));
                    to = InternetAddress.toString(message
                            .getRecipients(Message.RecipientType.TO));
                    cc = InternetAddress.toString(message
                            .getRecipients(Message.RecipientType.CC));
                    bcc = InternetAddress.toString(message
                            .getRecipients(Message.RecipientType.BCC));
                    subject = message.getSubject();
                    sentDate = message.getSentDate();
                    msgId = "";
                    if (from != null && replyTo != null && to != null
                            && subject != null && sentDate != null) {
                        System.out.println("From: " + from);
                        System.out.println("Reply-to: " + replyTo);
                        System.out.println("To: " + to);
                        System.out.println("Subject: " + subject);
                        System.out.println("Sent Date: " + sentDate);
                    }
                }

//              System.out.println("cccccccc <><> " + cc);
                // Getting Email List if TO OR CC OR BCC has  more than 1 email ID
                ArrayList<String> toEmailList = new ArrayList<String>();
                ArrayList<String> ccEmailList = new ArrayList<String>();
                ArrayList<String> bccEmailList = new ArrayList<String>();
                if(to != null){
                    toEmailList = Utility.getEmailList(to);
                }
                if(cc != null){
                    ccEmailList = Utility.getEmailList(to);
                }
                if(bcc!= null){
                    bccEmailList = Utility.getEmailList(to);
                }

//              String toEmailList[] = Utility.getEmailList(to);
//              String ccEmailList[] = Utility.getEmailList(cc);
//              String bccEmailList[] = Utility.getEmailList(bcc);
                // Get LEA EmailID
                boolean toCheck = false, ccCheck = false, bccCheck = false;
                int count = 0;
                if(toEmailList.size() != 0){
                    for(int k = 0;k<toEmailList.size();k++){
                        String email = toEmailList.get(k); 
                        String trackLeadId = Utility.splitEmail(email);
                        System.out.println("trackLeadId Value<><>" + trackLeadId);
                        String templeaEmailID[] = Utility.getFwdMailId(trackLeadId);
                        String leaEmailID = templeaEmailID[0];
                        String nodalOffID = templeaEmailID[1];
                        System.out.println("leaEmailID <><><> " + leaEmailID);
                        if(leaEmailID != null){
                            TestEmail.SendMail(leaEmailID, from, message, session, subject, msgId);
                            toCheck = true;
                            System.out.println("Mail Forwarded");
                            Utility.insertInfo(leaEmailID, trackLeadId, nodalOffID);
                        }

//                      else{
//                          message.setFlag(Flags.Flag.DELETED, true);
//                          System.out.println("Mail Deleted");
//                      }
                    }// ENd of K Loop
                }

                if(ccEmailList.size() != 0){
                    for(int l = 0;l<ccEmailList.size();l++){
                        String email = ccEmailList.get(l); 
                        String trackLeadId = Utility.splitEmail(email);
                        System.out.println("trackLeadId Value<><>" + trackLeadId);
                        String templeaEmailID[] = Utility.getFwdMailId(trackLeadId);
                        String leaEmailID = templeaEmailID[0];
                        String nodalOffID = templeaEmailID[1];
                        System.out.println("leaEmailID <><><> " + leaEmailID);
                        if(leaEmailID != null){
                            TestEmail.SendMail(leaEmailID, from, message, session, subject, msgId);
                            ccCheck = true;
                            System.out.println("Mail Forwarded");
                            Utility.insertInfo(leaEmailID, trackLeadId, nodalOffID);
                        }
                    }// ENd of L Loop
                }

                if(bccEmailList.size() != 0){
                    for(int m = 0;m<bccEmailList.size();m++){
                        String email = bccEmailList.get(m); 
                        String trackLeadId = Utility.splitEmail(email);
                        System.out.println("trackLeadId Value<><>" + trackLeadId);
                        String templeaEmailID[] = Utility.getFwdMailId(trackLeadId);
                        String leaEmailID = templeaEmailID[0];
                        String nodalOffID = templeaEmailID[1];
                        System.out.println("leaEmailID <><><> " + leaEmailID);
                        if(leaEmailID != null){
                            TestEmail.SendMail(leaEmailID, from, message, session, subject, msgId);
                            bccCheck = true;
                            System.out.println("Mail Forwarded");
                            Utility.insertInfo(leaEmailID, trackLeadId, nodalOffID);
                        }
                    }// ENd of M Loop
                }

                if(ccEmailList.size() == 0 || bccEmailList.size() == 0){
                    if(toCheck == false){
                        message.setFlag(Flags.Flag.DELETED, true);
                        System.out.println("Mail Deleted");

                    }
                }
                if(toCheck == false && ccCheck == false && bccCheck ==false){
                    message.setFlag(Flags.Flag.DELETED, true);
                    System.out.println("Mail Deleted");
                }

        } // End of i Loop
        inbox.close(true);
    }
}

and the console output for both cases are

when sent from outlook :

INSIDE IF ATTACHMENT
fileName <><> null

when sent from gmail:

INSIDE IF ATTACHMENT
fileName <><> "Attachment name".msg

any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:12:49+00:00Added an answer on June 9, 2026 at 8:12 pm

    You said “… when I send the same mail for outlook client …”; did you mean from Outlook client?

    It’s not required that attachments include a filename; perhaps Outlook isn’t setting a filename for a .msg attachment. You can use the msgshow.java demo program that comes with JavaMail to dump out the entire structure and content of the message. You also might want to examine the raw MIME content of the message to see exactly what Outlook is sending you.

    Also, you might want to look at the “isMimeType” method to simplify your code. A message attachment should have a MIME type of message/rfc822.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.