2

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

どんな助けでも大歓迎です。

4

1 に答える 1

2

あなたは「... Outlookクライアントに同じメールを送信すると...」と言いました。Outlook クライアントからということですか?

添付ファイルにファイル名を含める必要はありません。Outlook が .msg 添付ファイルのファイル名を設定していない可能性があります。JavaMail に付属の msgshow.java デモ プログラムを使用して、メッセージの構造と内容全体をダンプできます。また、メッセージの生の MIME コンテンツを調べて、Outlook が送信している内容を正確に確認することもできます。

また、コードを簡素化するために「isMimeType」メソッドを確認することもできます。メッセージの添付ファイルには、message/rfc822 の MIME タイプが必要です。

于 2012-08-13T06:41:02.747 に答える