JAVA解析eml格式的邮件
依赖
<!-- apache文件操作库 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- 邮件解析基本库 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- apache对javax.mail库的封装 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
代码
public class Test {
public static void main(String[] args) {
String dir = "D:/test/";
try(InputStream inputStream = new FileInputStream(dir+"test3.eml")) {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session, inputStream);
MimeMessageParser parser = new MimeMessageParser(msg).parse();
//邮件唯一id
String messageId = parser.getMimeMessage().getMessageID();
System.out.println(messageId);
//发件人
String from = parser.getFrom();
System.out.println(from);
//收件人列表
List<Address> toArray = parser.getTo();
System.out.println(toArray);
//抄送人列表
List<Address> ccArray = parser.getCc();
System.out.println(ccArray);
//密送人列表
List<Address> bccArray = parser.getBcc();
System.out.println(bccArray);
//邮件发送时间
Date sendDate = parser.getMimeMessage().getSentDate();
System.out.println(sendDate);
//邮件主题
String subject = parser.getSubject();
System.out.println(subject);
//获取正文
String html = parser.getHtmlContent();
System.out.println(html);
//所有文件,包括附件和正文中的图片等文件
List<DataSource> dataSources = parser.getAttachmentList();
//获取不到html内容时,则获取非html文本内容
if (html == null || html.length()==0) {
String plain = parser.getPlainContent();
System.out.println(plain);
}else {
//获取正文中的图片等文件
Collection<String> contentIds = parser.getContentIds();
for (String contentId : contentIds) {
DataSource contentFile = parser.findAttachmentByCid(contentId);
dataSources.remove(contentFile);
String name = contentFile.getName();
InputStream is = contentFile.getInputStream();
FileUtils.copyInputStreamToFile(is,new File(dir+name));
html = html.replace("cid:"+contentId,name);
}
FileUtils.writeStringToFile(new File(dir+"test.html"),html, Charset.defaultCharset());
}
for (DataSource dataSource : dataSources) {
String name = dataSource.getName();
System.out.println(name);
InputStream is = dataSource.getInputStream();
FileUtils.copyInputStreamToFile(is,new File(dir+name));
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
不积跬步无以至千里