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处的注释打开才可以过滤.

正确的操作步骤:
[b]1[/b].编写自己的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());
}
}



[b]2[/b]. 把这两个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这些包解压出来也放到这个目录下面, 不然会报找不到类的异常.

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

[b]4[/b].修改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,大功告成!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值