Java-用JavaMail读取eml文件

作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!

术语

  • eml Email Files,RFC 822 format

示例

可以用JavaMail对eml文件进行解析,示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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());
}
}