cn.featherfly.common.net.mail.client.MailReceiver Maven / Gradle / Ivy
package cn.featherfly.common.net.mail.client;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Store;
import javax.mail.search.AndTerm;
import javax.mail.search.IntegerComparisonTerm;
import javax.mail.search.MessageIDTerm;
import javax.mail.search.SearchTerm;
import cn.featherfly.common.lang.AssertIllegalArgument;
import cn.featherfly.common.net.NetException;
import cn.featherfly.common.net.mail.ImapMailServer;
import cn.featherfly.common.net.mail.Mail;
import cn.featherfly.common.net.mail.MailApiUtils;
import cn.featherfly.common.net.mail.MailCreator;
import cn.featherfly.common.net.mail.MailHandler;
import cn.featherfly.common.net.mail.MailUser;
import cn.featherfly.common.net.mail.Pop3MailServer;
/**
*
* 邮件接收客户端
*
* @author 钟冀
*/
public class MailReceiver extends AbstractMailClient{
/**
*
* @param mailUser mailUser
* @param pop3MailServer pop3MailServer
*/
public MailReceiver(MailUser mailUser, Pop3MailServer pop3MailServer) {
this(mailUser, pop3MailServer, null);
}
/**
*
* @param mailUser mailUser
* @param imapMailServer imapMailServer
*/
public MailReceiver(MailUser mailUser, ImapMailServer imapMailServer) {
this(mailUser, imapMailServer, null);
}
/**
* @throws MessagingException
*
*/
public MailReceiver(MailUser mailUser, Pop3MailServer pop3MailServer, Map props) {
super(mailUser, null, pop3MailServer, props);
AssertIllegalArgument.isNotNull(mailUser, "mailUser不能为空");
AssertIllegalArgument.isNotNull(pop3MailServer, "smtpServer不能为空");
}
/**
* @throws MessagingException
*
*/
public MailReceiver(MailUser mailUser, ImapMailServer imapMailServer, Map props) {
super(mailUser, null, imapMailServer, props);
AssertIllegalArgument.isNotNull(mailUser, "mailUser不能为空");
AssertIllegalArgument.isNotNull(imapMailServer, "smtpServer不能为空");
}
/**
*
* 获取指定folder指定messageId的邮件.
*
* @param messageId messageId
* @return 邮件
*/
public Mail get(String messageId) {
return get(messageId, mailCreator);
}
/**
*
* 获取指定folder指定messageId的邮件.
*
* @param messageId messageId
* @param mailCreator 邮件创建器
* @return 邮件
*/
public E get(String messageId, MailCreator mailCreator) {
AssertIllegalArgument.isNotEmpty(messageId, "messageId不能为空");
List list = search(new MessageIDTerm(messageId), mailCreator);
if (list.size() > 1) {
logger.warn("get messageId -> {} size > 0 , size -> {}", messageId, list.size());
}
if (list.size() > 0) {
return list.get(0);
} else {
return null;
}
}
/*
*
* 查询指定folder的邮件.
*
* @param searchTerm 查询条件,如果为空则返回所有
* @param mailCreator 邮件创建器
* @return 邮件
*/
private List search(SearchTerm searchTerm, MailCreator mailCreator){
AssertIllegalArgument.isNotNull(mailCreator, "mailCreator不能为空");
return search(searchTerm, 0, mailCreator, null);
}
/**
*
* 处理从指定folder的所有邮件.
*
*/
public void handle() {
handle(null);
}
/**
*
* 处理从指定folder查询出的邮件.
*
* @param searchTerm 查询条件,如果为空则返回所有
*/
public void handle(SearchTerm searchTerm) {
handle(searchTerm, mailCreator,mailHandler);
}
/**
*
* 处理从指定folder查询出的邮件.
*
* @param searchTerm 查询条件,如果为空则返回所有
* @param mailCreator 邮件创建器
* @param mailHandler 处理对象
*/
public void handle(SearchTerm searchTerm, MailCreator mailCreator, MailHandler mailHandler) {
AssertIllegalArgument.isNotNull(mailCreator, "mailCreator不能为空");
AssertIllegalArgument.isNotNull(mailHandler, "mailHandler不能为空");
search(searchTerm, 0, mailCreator, mailHandler);
}
/**
*
* 查询指定folder的邮件.
*
* @param searchTerm 查询条件,如果为空则返回所有
* @param mailCreator 邮件创建器
* @return 邮件
*/
private List search(SearchTerm searchTerm, int start, MailCreator mailCreator, MailHandler handler) {
AssertIllegalArgument.isNotNull(mailCreator, "mailCreator不能为空");
Store store = null;
try {
store = getStore();
return searchInStore(searchTerm, start, store, mailCreator, handler);
} catch (Exception e) {
throw new NetException(e);
} finally {
MailApiUtils.close(store, logger);
}
}
/**
*
* 查询指定folder的邮件.
*
* @param searchTerm 查询条件,如果为空则返回所有
* @param mailCreator 邮件创建器
* @return 邮件
*/
private List searchInStore(SearchTerm searchTerm, int start, Store store, MailCreator mailCreator, MailHandler handler) throws Exception {
AssertIllegalArgument.isNotNull(mailCreator, "mailCreator不能为空");
Folder folder = null;
int cacheSize = 0;
try {
List mails = new ArrayList();
// 获取指定文件箱
folder = getFolder(store);
folder.open(Folder.READ_ONLY);
Message[] messages = null;
if (searchTerm == null) {
messages = searchMessages(folder, new MessageNumberGeTerm(start));
} else {
messages = searchMessages(folder, new AndTerm(searchTerm, new MessageNumberGeTerm(start)));
}
for (int i=0, n=messages.length; i 0 && messageSize > allowCacheSize) {
throw new NetException("#mail.size.gt.cache", new Object[]{messageSize, allowCacheSize});
}
if (allowCacheSize > 0 && cacheSize > allowCacheSize) {
// 超过允许的缓存大小
logger.debug("从第[{}]封邮件开始,在第{}封邮件时累计缓存[{}]超过允许的缓存[{}]大小,重新连接",
new Object[]{start, i, cacheSize, allowCacheSize});
MailApiUtils.close(folder, false, logger);
mails.addAll(searchInStore(searchTerm, start + i, store, mailCreator, handler));
return mails;
}
if (message != null) {
E mail = mailCreator.create(message);
// 处理者没有的时候才加入集合返回
if (handler != null) {
handler.handle(mail);
} else {
mails.add(mail);
}
}
}
return mails;
} finally {
MailApiUtils.close(folder, false, logger);
}
}
/**
*
* 删除
*
* @param messageId
*/
public void delete(String messageId) {
flag(messageId, new Flags(Flag.DELETED));
}
/**
*
* 标记已读
*
* @param messageId
*/
public void seen(String messageId) {
flag(messageId, new Flags(Flag.SEEN));
}
/**
*
* 标记已回复
*
* @param messageId
*/
public void answer(String messageId) {
flag(messageId, new Flags(Flag.ANSWERED));
}
/**
*
* 标记已回复
*
* @param messageId
*/
public void recent(String messageId) {
flag(messageId, new Flags(Flag.RECENT));
}
/**
*
* 标记
*
* @param messageId
*/
public void flag(String messageId, String flag) {
flag(messageId, new Flags(flag));
}
/*
*
* 标记
*
* @param messageId messageId
* @param flag 标记
*/
private void flag(String messageId, Flags flags) {
Store store = null;
Folder folder = null;
try {
SearchTerm searchTerm = new MessageIDTerm(messageId);
// 获取邮箱的存储
store = getStore();
// 获取指定文件箱
folder = getFolder(store);
folder.open(Folder.READ_WRITE);
Message[] messages = searchMessages(folder, searchTerm);
for (int i=0, n=messages.length; i mailCreator;
private MailHandler mailHandler;
private int allowCacheSize = -1;
private String folder = "INBOX";
/**
* 返回folder
* @return folder
*/
public String getFolder() {
return folder;
}
/**
* 设置folder
* @param folder folder
*/
public void setFolder(String folder) {
this.folder = folder;
}
/**
* 返回mailCreator
* @return mailCreator
*/
public MailCreator getMailCreator() {
return mailCreator;
}
/**
* 设置mailCreator
* @param mailCreator mailCreator
*/
public void setMailCreator(MailCreator mailCreator) {
this.mailCreator = mailCreator;
}
/**
* 返回mailHandler
* @return mailHandler
*/
public MailHandler getMailHandler() {
return mailHandler;
}
/**
* 设置mailHandler
* @param mailHandler mailHandler
*/
public void setMailHandler(MailHandler mailHandler) {
this.mailHandler = mailHandler;
}
/**
* 返回allowCacheSize
* @return allowCacheSize
*/
public int getAllowCacheSize() {
return allowCacheSize;
}
/**
* 设置allowCacheSize
* @param allowCacheSize allowCacheSize
*/
public void setAllowCacheSize(int allowCacheSize) {
this.allowCacheSize = allowCacheSize;
}
}
class MessageNumberGeTerm extends IntegerComparisonTerm {
private static final long serialVersionUID = -5379625829658623812L;
/**
* Constructor.
*
* @param number the Message number
*/
public MessageNumberGeTerm(int number) {
super(GE, number);
}
/**
* {@inheritDoc}
*/
@Override
public boolean match(Message msg) {
int msgno;
try {
msgno = msg.getMessageNumber();
} catch (Exception e) {
return false;
}
return super.match(msgno);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MessageNumberGeTerm))
return false;
return super.equals(obj);
}
}
class MessageNumberLtTerm extends IntegerComparisonTerm {
private static final long serialVersionUID = -5379625829658623812L;
/**
* Constructor.
*
* @param number the Message number
*/
public MessageNumberLtTerm(int number) {
super(LT, number);
}
/**
* {@inheritDoc}
*/
@Override
public boolean match(Message msg) {
int msgno;
try {
msgno = msg.getMessageNumber();
} catch (Exception e) {
return false;
}
return super.match(msgno);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MessageNumberGeTerm))
return false;
return super.equals(obj);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy