简体   繁体   中英

Java mail Api ,unable to read “.msg attachment” from outlook client

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM