`
wander312
  • 浏览: 213830 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JAMES 垃圾邮件过滤

阅读更多
    最近公司发布了一个JAMES邮件服务器, 第二天早一来, 发现spool里面有几万的垃圾邮件.
在网上仔细查了一下, 一般都是自定义一个matcher和一个mailet, 如:
package com.eazitour.mailet;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import javax.mail.MessagingException;
import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;

public class FilterSenderMatcher extends GenericMatcher {
	private static  String[] hosts = null;
	@Override
	public Collection match(Mail mail) throws MessagingException {
		//System.out.println("getCondition:" + getCondition());
		if (hosts == null && getCondition() != null) {
			hosts = getCondition().split(",");
		}
		if (hosts == null || hosts.length == 0 || getCondition() == null) {
			return mail.getRecipients();
		}
		MailAddress mailAddress = mail.getSender();
		//System.out.println("mailAddress.toString():" + mailAddress.toString());
		for (String strTemp : hosts) {
			if (mailAddress.toString().toLowerCase().indexOf(strTemp) != -1) {
				//System.out.println(mailAddress.toString().toLowerCase().indexOf(strTemp));
				return mail.getRecipients();
			}
		}
		//mail.getRecipients().clear();//code1
        return null;
	}
}


可发现这种做法根本就过滤不了后缀是本服务器域名的邮件, 但JAMES包下的:org\apache\james\transport\matchers很多matche是这种写法. 经过尝试, 得把code1处的注释打开才可以过滤.

正确的操作步骤:
1.编写自己的Matcher, 上面一段code(把code1处的注释打开), 和Mailet.
 
package com.eazitour.mailet;
import javax.mail.MessagingException;
import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
public class FilterSenderMailet extends GenericMailet {
	@Override
	public void service(Mail mail) throws MessagingException {
//		MailAddress ma = mail.getSender();   
//        System.out.println("sender:"+ma.toInternetAddress().toString()); 
	}
}

  


2. 把这两个JAVA类打包成JAR包, 放到james-2.3.2\apps\james\SAR-INF\lib目录下, 同时也要把james-2.3.2\apps\james.sar里面的james-2.3.2.jar,mail-1.4.1.jar,mailet-2.3.jar,mailet-api-2.3.jar这些包解压出来也放到这个目录下面, 不然会报找不到类的异常.

3.修改config.xml文件, 在<mailetpackages>后面添加:<mailetpackage>com.eazitour.mailet</mailetpackage>, 在<matcherpackages>后面添加:<matcherpackage>com.eazitour.mailet</matcherpackage>, 即是刚才的那两个类的包名.

4.修改config.xml文件, 添加过滤,在<spoolmanager>下面,<mailet match="All" class="PostmasterAlias"/>的后面添加:<mailet match="FilterSenderMatcher=@eazitour.com,@eazitour.cn"  class="FilterSenderMailet"/>, FilterSenderMatcher即为Mathcer的类的名字, FilterSenderMailet也mailet的类的名字, 它会在上面配置的包下面去找这两个类, 先通过matcher, 如果matcher返回的不是null的collection, 刚会下一步调用mailet; 如果matcher返回null, 但mail.getRecipients()不为null, 邮件会发送成功, 但不调用mailet, 所以code1处的注释要打开才不会发送.

OK,大功告成!

分享到:
评论
2 楼 wzl19900210 2014-08-12  
我想咨询下请问getCondition()方法在哪啊
可以贴出来吗
1 楼 hualikejava 2011-04-02  

相关推荐

Global site tag (gtag.js) - Google Analytics