Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java邮件Api,无法读取";。味精附件“;从outlook客户端_Java_Jakarta Ee_Jakarta Mail - Fatal编程技术网

Java邮件Api,无法读取";。味精附件“;从outlook客户端

Java邮件Api,无法读取";。味精附件“;从outlook客户端,java,jakarta-ee,jakarta-mail,Java,Jakarta Ee,Jakarta Mail,我有一个使用javamail api读取电子邮件的类,我面临的问题是,当我从gmail发送包含.msg附件的邮件时,我的代码可以很容易地读取它,但是当我为outlook客户端发送相同的邮件时,.msg附件不会显示 我的班级代码如下: public class TestEmail { static int count = 5; static String ipAddress[] = Utility.getipAddress(); static String auth[] =

我有一个使用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);
    }
}
public类邮件{
静态整数计数=5;
静态字符串ipAddress[]=Utility.getipAddress();
静态字符串auth[]=Utility.getEmailPasswd();
私有静态最终字符串SMTP_AUTH_USER=AUTH[0];
私有静态最终字符串SMTP_AUTH_PWD=AUTH[1];
私有静态字符串defaultServer=ipAddress[1];
私有静态字符串domainServer=ipAddress[0];
公共静态void main(字符串[]args)引发异常{
/***************变量声明*********************/
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(“域服务器”+域服务器);
int noOfEmail=0;
字符串从“”,replyTo=“”,到“”,subject=“”,cc=“”,bcc=“”,msgId=“”;
日期sentDate=新日期();
字符串fileName=“”;
/**************变量声明结束****************/
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”);
道具放置(“mail.imap.port”、“143”);
put(“mail.smtp.localhost”,defaultServer);
//props.put(“mail.debug”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
Session Session=Session.getDefaultInstance(props,newMailAuthenticator());
//Store Store=session.getStore(“pop3”);
Store Store=session.getStore(“imap”);
connect(domainServer,null,null);
//session.setDebug(true);
/************阅读收件箱*********/
文件夹收件箱=store.getFolder(“收件箱”);
收件箱。打开(文件夹。读写);
/************电邮数目*********/
noOfEmail=inbox.getMessageCount();
System.out.println(“邮件数量”+noOfEmail);
/********搜索未读消息********/
FlagTerm ft=新FlagTerm(新标志(Flags.Flag.SEEN),false);
消息消息[]=收件箱搜索(ft);
System.out.println(“未读邮件总数”+邮件长度);
/***********横穿Messase*******/
for(int i=0;ioutlook客户端的

附件不要求包含文件名;可能Outlook没有为.msg附件设置文件名。您可以使用JavaMail附带的msgshow.java演示程序转储邮件的整个结构和内容。您还可能希望检查邮件的原始MIME内容,以查看Outlook到底是什么送你去


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

AFAIK:Outlook邮件的序列化版本(
*.msg
)现在是公共格式,但开放源代码不完整。请参阅此处的详细信息:
INSIDE IF ATTACHMENT
fileName <><> null
INSIDE IF ATTACHMENT
fileName <><> "Attachment name".msg