/**
* 通用邮件解析工具
*
* @author zeemoo
* @date 2019/01/18
*/
public class MailItemParser {
public static final String IMPORT_FOLDER = "手动导入";
/**
* eml文件后缀
*/
private static final String EML_SUFFIX = ".eml";
/**
* 解析通用邮件内容
*
* @param mailItem
* @param targetDir
* @return
*/
public static UniversalMail parseMail(MailItem mailItem, String targetDir) throws MailPlusException {
UniversalMail universalMail = null;
if (mailItem.getPop3Message() != null) {
POP3Message pop3Message = mailItem.getPop3Message();
universalMail = parseMimeMessage(pop3Message, targetDir);
if (universalMail.getHasAttachment()) {
List universalAttachments = parseAttachment(pop3Message, targetDir + "/" + universalMail.getUid());
universalMail.setAttachments(universalAttachments);
}
String emlPath = saveMimiMessageAsLocalEml(pop3Message, targetDir + "/" + universalMail.getUid());
universalMail.setEmlPath(emlPath);
} else if (mailItem.getImapMessage() != null) {
IMAPMessage imapMessage = mailItem.getImapMessage();
universalMail = parseMimeMessage(imapMessage, targetDir);
if (universalMail.getHasAttachment()) {
List universalAttachments = parseAttachment(imapMessage, targetDir + "/" + universalMail.getUid());
universalMail.setAttachments(universalAttachments);
}
String emlPath = saveMimiMessageAsLocalEml(imapMessage, targetDir + "/" + universalMail.getUid());
universalMail.setEmlPath(emlPath);
} else if (mailItem.getExchangeMessage() != null) {
universalMail = parseExchangeMail(mailItem.getExchangeMessage());
//解析附件
if (universalMail.getHasAttachment()) {
try {
EmailMessage exchangeMessage = mailItem.getExchangeMessage();
exchangeMessage.load(new PropertySet(EmailMessageSchema.MimeContent, EmailMessageSchema.AllowedResponseActions, EmailMessageSchema.Attachments));
List attachments = parseAttachment(exchangeMessage.getAttachments(), targetDir + "/" + universalMail.getUid());
universalMail.setAttachments(attachments);
} catch (Exception e) {
e.printStackTrace();
throw new MailPlusException(e.getMessage());
}
}
//保存到本地
String emlPath = saveExchangeMailAsLocalEml(mailItem.getExchangeMessage(), targetDir + "/" + universalMail.getUid());
universalMail.setEmlPath(emlPath);