论坛首页 Java企业应用论坛

JavaMail判断新邮件

浏览 13710 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-17  
mimeMessage.getReceivedDate();为什么获取不到接收时间呢?打印时间空的
mimeMessage.getSentDate();可以拿到邮件的发送时间
我现在在写一个接收邮件新的程序 协议pop3 判断新邮件,以后读的时候只读新邮件的内容,不打印所有的邮件信息
我不想通过邮件的uid来判断,因为保存邮件的uid浪费空间,什么时候该删也不知道。通过uid来判断读来读去的,n*n次方
我就想通过邮件的接收时间来判断,只保存一个接收时间在本地,下次从保存时间开始读,,时间也会更新,也就是必须要拿到服务器端的邮件信息来作判断。在本地做标记没什么作用
通过邮件的发送时间来判断根本没作用,有的邮件可能到达的时间比较晚。。
我就奇怪啦,同个类两个方法,为什么接收时间就获取不到呢,哪位高手行行好啊,这个新邮件问题,弄了几天了还没弄出来,项目经理要叨人啦 几乎搜遍了整个网络 网上也没什么人给出一个好的实现思路,大多说通过uid.各位大虾有什么更好的实现方式,说来参考一下,非常急:小弟在此谢过了。。。Q : 294810182
Mail: jiangshulin013@163.com
我尝试过很多网友的方法
Flags.seen 设置为true不行
Flags.getFlags.toString() equals来判断不管是新旧
打印全是这个:javax.mail.Flags0
没有所谓的javax.mail.Flags10
javax.mail.Flags20
记录程序的最后访问时间到本记,下次判断是可以,但是有一个问题,比如我10点上去读了1次,有10封新邮件,我只读了5封,下次读的是这个时间以后的邮件,那其余的5封不是漏读了.
声明一下:服务器的邮件是拿到本地保存的,不牵涉到数据库 
只是用纯Java写一个application  但现在每次运行这个接收程序的时候会把服务器收件箱里所有的邮件都拿下来
要求是以后只读取新邮件的信息,不重读,不漏读


以下是JMail api里面对这个方法的描述
getReceivedDate
public Date getReceivedDate()
                     throws MessagingExceptionReturns the Date on this message was received. Returns null if this date cannot be obtained.
Note that RFC 822 does not define a field for the received date. Hence only implementations that can provide this date need return a valid value.

This implementation returns null.


Specified by:
getReceivedDate in class Message
Returns:
the date this message was received
Throws:
MessagingException

public Date getSentDate()
                 throws MessagingExceptionReturns the value of the RFC 822 "Date" field. This is the date on which this message was sent. Returns null if this field is unavailable or its value is absent.
This implementation uses the getHeader method to obtain the requisite header field.


Specified by:
getSentDate in class Message
Returns:
The sent Date
Throws:
MessagingException

   发表时间:2010-04-17  
pop3 是无法告诉你是否为新邮件的

你肯定要拿uidl并且保存  然后下次比对

如果你不保存uid 不能实现吧...
话说的太绝了...也许有逆天强人能解决

imap 才支持javamail的 SEEN DELETED等
0 请登录后投票
   发表时间:2010-04-18  
我知道啊,但是有没有更好的实现方式,尽量从性能上考虑。。
0 请登录后投票
   发表时间:2010-04-18  
虽说uid只是一个长的字符串占用不了多少空间
,假如有1000封邮件,甚至更多,难道就让它一直保存在硬盘里面..
0 请登录后投票
   发表时间:2010-04-19  
那个占不了多少空间的。就跟我做一个rss阅读器,你怎么判断rss列表中的新文章?时间是不行的,有的rss更本不输出时间,有的是当前时间。只能用文章内容的hash结果比较。
0 请登录后投票
   发表时间:2010-04-19  
jiangshulin013 写道
我知道啊,但是有没有更好的实现方式,尽量从性能上考虑。。

没有更好的办法,考虑性能问题,也只能从实现方法上考虑最优算法。
0 请登录后投票
   发表时间:2010-04-19   最后修改:2010-04-19
提供一个思路:
(1)可以从POP3Message的Head中取得Date,分析Date可以拿到ReceivedDate
(2)扩展javax.mail.search.SearchTerm,实现一个Pop3ReceivedDateTerm
public class Pop3ReceivedDateTerm extends SearchTerm {

	private static final long serialVersionUID = 4363009965326553239L;

	private int comparison;

	private Date date;

	public Pop3ReceivedDateTerm(int comparison, Date date) {
		this.comparison = comparison;
		this.date = date;
	}

	@Override
	public boolean match(Message message) {
		if ((message instanceof POP3Message) == false) {
			throw new IllegalStateException("The message is not POP3Message!");
		}
		if (this.date == null) {
			throw new IllegalArgumentException("The receivedDate could not be null!");
		}

		Date receivedDate = MailUtil.getPop3ReceivedDate((POP3Message) message);
		if (receivedDate == null) {
			return false;
		} else if (ComparisonTerm.EQ == this.comparison) {
			return this.date.equals(receivedDate);
		} else if (ComparisonTerm.GE == this.comparison) {
			return this.date.before(receivedDate) || this.date.equals(receivedDate);
		} else if (ComparisonTerm.GT == this.comparison) {
			return this.date.before(receivedDate);
		} else if (ComparisonTerm.LE == this.comparison) {
			return this.date.after(receivedDate) || this.date.equals(receivedDate);
		} else if (ComparisonTerm.LT == this.comparison) {
			return this.date.after(receivedDate);
		} else if (ComparisonTerm.NE == this.comparison) {
			return !this.date.equals(receivedDate);
		}
		return false;
	}

}

public static Date getPop3ReceivedDate(POP3Message message){
		try {
			String[] strs = message.getHeader("Date");			
			if(strs==null || strs.length != 1){
				return null;
			}
			
			String date = strs[0];
			DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",Locale.US);
			return dateFormat.parse(date);
		} catch (Exception e) {
			return null;
		}
	}

(3)利用folder.search(searchTerm)取得新邮件
0 请登录后投票
   发表时间:2010-04-19  
楼主你这需求太扯了,读没读过用户说了算,你这属于自己猜用户习惯了。outlook鼠标点上邮件后还得判断几秒内不算读过呢,每个邮件存个hash能占多少空间?跟一封邮件占的空间比起来算什么,邮件都存了还不舍得这么点
0 请登录后投票
   发表时间:2010-04-19  
fangin 写道
楼主你这需求太扯了,读没读过用户说了算,你这属于自己猜用户习惯了。outlook鼠标点上邮件后还得判断几秒内不算读过呢,每个邮件存个hash能占多少空间?跟一封邮件占的空间比起来算什么,邮件都存了还不舍得这么点


追求完美挺好...

实现好了  我直接当伸手党就可以了....
0 请登录后投票
   发表时间:2010-04-19  
什么都不存, 你意思是想空手套白狼?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics