当访问message / rfc822附件的innear主题时,我们获得Null

时间:2017-02-13 10:05:34

标签: java javamail mime-message

Input

https://i.stack.imgur.com/MnfDs.png

在上图中,我们提到了输入文件,它以eml。

的形式提供输入
 MimeMessageParser mmparser = new MimeMessageParser(message);        
mmparser.parse();    
System.out.println("Subject1: " + mmparser.getSubject());       
List<DataSource> attachmentList = mmparser.getAttachmentList();     
System.out.println("Number of attachment " + attachmentList.size());     
DataSource ds1 = mmparser.getAttachmentList().get(0);           
System.out.println("Content Type:" + ds1.getContentType());       
final Session mail_session = Session.getDefaultInstance(new java.util.Properties());                           
final MimeMessage mimeMsgAtt = new MimeMessage(mail_session, ds1.getInputStream());
MimeMessageParser mmparserAttachment = new MimeMessageParser(mimeMsgAtt);      
MimeMessageParser mmparser1 = new MimeMessageParser(mmparserAttachment.getMimeMessage());                             
mmparser1.parse();                                 
System.out.println("Subject2: " + mmparser1.getSubject());     
List<DataSource> attachNum = mmparser1.getAttachmentList();       
System.out.println("Number of attachment:" +attachNum.size());       
DataSource dsX2 = mmparser1.getAttachmentList().get(0);       
final MimeMessage mimeMsgAttX1 = new MimeMessage(mail_session, dsX2.getInputStream());        
MimeMessageParser mmparserAttachmentX2 = new MimeMessageParser(mimeMsgAttX1);     
MimeMessageParser mmparser2 = new MimeMessageParser(mmparserAttachmentX2.getMimeMessage());                                
mmparser2.parse();       
// THE PROBLEM, subject is null      
System.out.println("Subject3: " + mmparser2.getSubject());     


> output

 :Subject1: Undeliverable: GEET - Downtime Notification for E411   
Number of attachment 1      
Content Type: message/rfc822     
Subject2: Undeliverable: GEET - Downtime Notification for E411     
Number of attachment: 2      
Subject3: null     

在这里,我们无法访问第三个主题 GEET - E411的停机时间通知。我无法找到我的错误,所以请帮助我。

1 个答案:

答案 0 :(得分:1)

根据您添加到此unrelated JavaMail bug report的数据,问题可能是邮件格式不正确。附加的消息有这个标题:

Content-Type: text/html; charset="'us-ascii'"

charset值被引用两次,这在查找charset转换器来解码内容时会导致失败。您可以使用technique described in the JavaMail FAQ to handle this bogus charset value

我对MimeMessageParser一无所知,但即使忽略JavaMail上面的charset问题,也可以使用MimeMessage类和msgshow.java示例程序正确解析消息:

$ java msgshow -m -s < DemoEmail.eml
This is the message envelope
---------------------------
FROM: Microsoft Outlook <MicrosoftExchange329e71ec88ae4615bbc36ab6ce41109e@pocmchase.com>
REPLY TO: Microsoft Outlook <MicrosoftExchange329e71ec88ae4615bbc36ab6ce41109e@pocmchase.com>
TO: rasem017@eddbas-pyr20v.svr.emea.pocmchase.net
SUBJECT: Undeliverable: GEET - Downtime Notification for E411
SendDate: Mon Jan 23 06:34:12 PST 2017
FLAGS: 
X-Mailer NOT available
CONTENT-TYPE: multipart/mixed; 
        boundary=_f21b488c-e1c0-4de8-9c64-2579a8c7a35d_
This is a Multipart
---------------------------
  CONTENT-TYPE: text/plain; charset=us-ascii
  This is plain text
  ---------------------------
  CONTENT-TYPE: message/rfc822
  This is a Nested Message
  ---------------------------
    This is the message envelope
    ---------------------------
    FROM: Microsoft Outlook <MicrosoftExchange329e71ec88ae4615bbc36ab6ce41109e@pocmchase.com>
    REPLY TO: Microsoft Outlook <MicrosoftExchange329e71ec88ae4615bbc36ab6ce41109e@pocmchase.com>
    TO: rasem017@eddbas-pyr20v.svr.emea.pocmchase.net
    SUBJECT: Undeliverable: GEET - Downtime Notification for E411
    SendDate: Mon Jan 23 06:34:12 PST 2017
    FLAGS: 
    X-Mailer NOT available
    CONTENT-TYPE: multipart/report; report-type=delivery-status; 
        boundary=_647f43cc-ceff-4427-bc28-e7366392af1f_
    This is a Multipart
    ---------------------------
      CONTENT-TYPE: multipart/alternative; differences=Content-Type; 
        boundary=_0208790d-52b7-4d11-b3c4-37e958eea557_
      This is a Multipart
      ---------------------------
        CONTENT-TYPE: text/plain; charset=us-ascii
        This is plain text
        ---------------------------
        CONTENT-TYPE: text/html; charset=us-ascii
        ---------------------------
      CONTENT-TYPE: message/delivery-status
      ---------------------------
      CONTENT-TYPE: message/rfc822
      This is a Nested Message
      ---------------------------
        This is the message envelope
        ---------------------------
        FROM: geet.alerts@pocmchase.com
        REPLY TO: geet.alerts@pocmchase.com
        TO: geet.alerts@pocmchase.com
        SUBJECT: GEET - Downtime Notification for E411
        SendDate: Mon Jan 23 06:33:42 PST 2017
        FLAGS: 
        X-Mailer NOT available
        CONTENT-TYPE: text/html; charset='us-ascii'
        ---------------------------