2017-02-13 25 views
0

Input我們訪問消息/ RFC822附件的innear受試者

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

基於你加入到這一unrelated JavaMail bug report數據時,問題可能是不正確地格式化該消息。附加的消息具有此標題:

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

的字符集值是引用兩次,尋找字符集轉換器將內容進行解碼時引起的故障。您可能可以使用technique described in the JavaMail FAQ to handle this bogus charset value

我不知道MimeMessageParser什麼,但竟然無視上述JavaMail的字符集問題,能夠正確地解析消息使用MimeMessage類和msgshow.java示例程序:

$ java msgshow -m -s < DemoEmail.eml 
This is the message envelope 
--------------------------- 
FROM: Microsoft Outlook <[email protected]> 
REPLY TO: Microsoft Outlook <[email protected]> 
TO: [email protected] 
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 <[email protected]> 
    REPLY TO: Microsoft Outlook <[email protected]> 
    TO: [email protected] 
    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: [email protected] 
     REPLY TO: [email protected] 
     TO: [email protected] 
     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' 
     --------------------------- 
+0

先生,我得到的第三來自msgshow.java。但我沒有通過MimeMessageParser訪問。我還在eml文件中更改了「CONTENT-TYPE:text/plain; charset = us-ascii」。我想通過MimeMessageParser得到結果。 – Ashish

+0

對不起,我幫不了你。這不是JavaMail的一部分,我對此一無所知。 –