一个邮件提醒系统要实现的功能,当拿到邮件列表后需要自己判断,大多是通过邮件的UID进行判断,注意UID与MessageID并非一个东西,UID是邮箱用来标识你这个账户的每一封邮件的东西,而MessageID是发送邮件的时候生成的唯一ID,也有可能发送没有你的接收邮箱自己生成,或者是javamail生成的,总是取messageid需要下载邮件的头,这样有联网操作会很慢的,所以我们只需要存储下来uid就好了,记得保存的时候按照邮箱存储,因为不同的邮箱uid会重复的,至于规律最好别去参考有增的就有减的,所以还是安心的遍历吧,反正很快
拷贝我刚写的部分代码作为参考
final Properties props = new Properties();
if (ssl) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
}
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(type);
store.connect(host, port, email, password);
logger.info("login email:{} server:", emailAccountBean.toString(),
serverBean.toString());
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
try {
if (folder instanceof POP3Folder) {
POP3Folder inbox = (POP3Folder) folder;
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
MimeMessage mimeMessage = (MimeMessage) messages[i];
String uid = inbox.getUID(mimeMessage);
if (!emailUidDB.get(userSession.getUserId(), uid)) {
receive(mimeMessage, uid);
}
}
} else if (folder instanceof IMAPFolder) {
IMAPFolder inbox = (IMAPFolder) folder;
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
MimeMessage mimeMessage = (MimeMessage) messages[i];
String uid = Long.toString(inbox.getUID(mimeMessage));
if (!emailUidDB.get(userSession.getUserId(), uid)) {
receive(mimeMessage, uid);
}
}
} else {
logger.error("no have this folder {}", folder);
}
emailAccountBean.setTime(System.currentTimeMillis());
} finally {
folder.close(false);
store.close();
}
大家注意这个方法
String uid = inbox.getUID(mimeMessage);
要先强制转换
IMAPFolder inbox = (IMAPFolder) folder;
和
POP3Folder inbox = (POP3Folder) folder;
是哪个自己判断就好了,这样取的UID是不需要下载每一封邮件的这样速度很高,即使是几千封邮件也很快完成千万不要用mimeMessage.getMessageID();这个方法,这个方法会去下载邮件头,是一个很耗时的过程不推荐验完首封邮件和尾封邮件就跳出循环的方法,因为遇到过新邮件夹在中间的时候
今天的发现 之前都是这么写final Properties props = new Properties();
if (ssl) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
}
Session session = Session.getDefaultInstance(props, null);
现在发现这样写不对,其实加密是不需要特殊处理的,只需要把 type换成pop3s和imaps就可以了,还有就是session如果不带有用户和服务器信息,全程序公用一个就可以了就这样生成一个公用的就可以了
private final Session session = Session.getDefaultInstance(System.getProperties());
而且之前那样写用default拿session每次都拿到同一个新传入的props也会失效的,所以之前犯了大错误 呵呵