从服务器中获取到邮件的eml文件,在java代码中进行解析。包括了发件人地址,收件人(抄送人、密送人)地址,邮件主题和邮件发送时间以及邮件正文等信息,其中邮件正文信息是通过
BodyPart bodyPart = multipart.getBodyPart(idx);
中的bodyPart.isMimeType(***)来判断的。
import lombok.extern.log4j.Log4j2;
import javax.inject.Named;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.apache.commons.io.IOUtils.copy;
/**
* @program:biz-trace
* @description:接收邮件解析类
* @author:zhaoshaojie
* @create:2020-9-23
*/
@Log4j2
@Named
public class MailReader {
private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static StringBuffer bodyText = new StringBuffer();
/**
* 解析文件
* @param emlPath
* @throws Exception
*/
//http://blog.csdn.net/aassdd_zz/article/details/8204344
public Map<String ,Object> parserFile(String emlPath) throws Exception{
log.info("获取邮件地址为:"+emlPath);
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
InputStream inMsg = new FileInputStream(emlPath);
MimeMessage msg = new MimeMessage(session, inMsg);
return parseEml(msg);
}
private Map<String ,Object> parseEml(MimeMessage msg) throws Exception {
Map<String ,Object> map = new HashMap<String ,Object>();
//获取邮件的message-ID
map.put("id",msg.getMessageID());
//发件人地址
Address[] froms = msg.getFrom();
if(froms != null){
InternetAddress addr = (InternetAddress) froms[0];
map.put("sender", addr.getAddress());
}
//收件人地址 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同 "to"----收件人 "cc"---抄送人地址 "bcc"---密送人地址
Map<String, List<String>> receivermap = new HashMap<>();
InternetAddress []address_TO = (InternetAddress[])msg.getRecipients(Message.RecipientType.TO);//收件人
InternetAddress []address_CC = (InternetAddress[])msg.getRecipients(Message.RecipientType.CC);//抄送人
InternetAddress []address_BCC = (InternetAddress[])msg.getRecipients(Message.RecipientType.BCC);//密送人
if(address_TO != null){
List<String> list = new ArrayList<>();
for (int i=0;i<address_TO.length;i++){
list.add(address_TO[i].getAddress());
}
receivermap.put("receipt",list);
}
if(address_CC != null){
List<String> list = new ArrayList<>();
for (int i=0;i<address_CC.length;i++){
list.add(address_TO[i].getAddress());
}
receivermap.put("copy",list);
}
if(address_BCC != null){
List<String> list = new ArrayList<>();
for (int i=0;i<address_BCC.length;i++){
list.add(address_TO[i].getAddress());
}
receivermap.put("blind",list);
}
map.put("receivers",receivermap);
//邮件主题
map.put("title",msg.getSubject());
//正文
map.put("text",parseMultipart((Multipart)msg.getContent()));
//邮件发送日期
map.put("mailTime",format.format(msg.getSentDate()));
return map;
}
/**
* 正文内容
* @param multipart
* @throws Exception
*/
private String parseMultipart(Multipart multipart) throws Exception{
int count = multipart.getCount();
for (int idx = 0; idx < count; idx++) {
BodyPart bodyPart = multipart.getBodyPart(idx);
if (bodyPart.isMimeType("text/plain")) {
bodyText.append(bodyPart.getContent());
} else if (bodyPart.isMimeType("text/html")) {
bodyText.append(bodyPart.getContent());
} else if (bodyPart.isMimeType("multipart/*")) {
Multipart mpart = (Multipart) bodyPart.getContent();
parseMultipart(mpart);
} else if (bodyPart.isMimeType("application/octet-stream")) {
String disposition = bodyPart.getDisposition();
if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {
InputStream is = bodyPart.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
copy(is, baos);
bodyText.append(new String(baos.toByteArray()));
}
}
}
return bodyText.toString().replaceAll("<[.[^>]]*>","");
}
}