java pop邮件 源码_POP3、IMAP4和Exchange收取邮件的JAVA开发详解(二)——代码解析和详细开发过程...

这是一个关于如何使用Java解析POP3、IMAP4和Exchange邮件的详细教程。代码解析了如何获取邮件内容、附件,并将邮件保存为本地eml文件。涵盖通用邮件解析工具、邮件附件处理和不同邮件协议的处理方式。
摘要由CSDN通过智能技术生成

/**

* 通用邮件解析工具

*

* @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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值