Java邮件Api,无法从Outlook客户端读取“.msg附件”

时间:2012-08-13 05:55:34

标签: java java-ee javamail

我有一个使用javamail api读取电子邮件的类,我面临的问题是,当我从gmail发送包含.msg附件的邮件时,我的代码可以轻松读取它,但是当我发送它时Outlook客户端的邮件,“。msg”附件没有显示

我班的代码如下:

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);
    }
}

并且两种情况的控制台输出都是

从outlook发送时:

INSIDE IF ATTACHMENT
fileName <><> null

从gmail发送时:

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

你说“......当我为Outlook客户端发送同样的邮件时......”;你的意思是来自 Outlook客户端吗?

附件不需要包含文件名;也许Outlook没有为.ms​​g附件设置文件名。您可以使用JavaMail附带的msgshow.java演示程序来转储消息的整个结构和内容。您还可能需要检查邮件的原始MIME内容,以确切了解Outlook向您发送的内容。

此外,您可能希望查看“isMimeType”方法以简化代码。邮件附件的MIME类型应为message / rfc822。