使用JavaMail解析EML文件详解编程语言

Java

当我们在outlook中保存一个邮件是可以存成eml格式,这种格式是标准的邮件格式. 这种文件可以用JavaMail来解析。

import java.util.*; 
import java.io.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
 
public class ReadEmail { 
 
   public static void main(String args[]) throws Exception{ 
       display(new File("C://temp//message.eml")); 
 
   } 
 
   public static void display(File emlFile) throws Exception{ 
        Properties props = System.getProperties(); 
        props.put("mail.host", "smtp.dummydomain.com"); 
        props.put("mail.transport.protocol", "smtp"); 
 
        Session mailSession = Session.getDefaultInstance(props, null); 
        InputStream source = new FileInputStream(emlFile); 
        MimeMessage message = new MimeMessage(mailSession, source); 
 
        System.out.println("Subject : " + message.getSubject()); 
        System.out.println("From : " + message.getFrom()[0]); 
        System.out.println("--------------"); 
        System.out.println("Body : " +  message.getContent()); 
    } 
}

eml文件格式说明 :

X-Mozilla-Status: 0001 
X-Mozilla-Status2: 00000000 
Received: from tomts25-srv.bellnexxia.net  
        (tomts25.bellnexxia.net [209.226.175.188]) 
    by tactika.com (8.9.3/8.9.3) with ESMTP id NAA07621 
    for ; Sun, 1 Feb 2004 13:25:33 -0500 (EST) 
Date: Sun, 01 Feb 2004 13:31:40 -0500 
From: real gagnon  
Reply-To: [email protected] 
User-Agent: Mozilla/5.0  
   (Windows; U; Windows NT 5.1; en-US; rv:1.4)  
   Gecko/20030624 Netscape/7.1 (ax) 
X-Accept-Language: en-us, en 
MIME-Version: 1.0 
To: [email protected] 
Subject: Example for HowTo 
Content-Type: text/plain; charset=us-ascii; format=flowed 
Content-Transfer-Encoding: 7bit 
X-UIDL: oP#!!c]^!!1;[email protected]"! 
 
This is an example for HowTo

通过上面的程序解析后的输出如下:

Subject : Example for HowTo 
From : real gagnon  
-------------- 
Body : 
This is an example for HowTo

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11047.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论