james监听邮件处理 Custom Mailet

新建一个maven项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.linewell</groupId>
    <artifactId>mail-filter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mailet.version>3.8.1-SNAPSHOT</mailet.version>
        <hutool.version>5.7.19</hutool.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.james</groupId>
            <artifactId>apache-mailet-base</artifactId>
            <version>${mailet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.james</groupId>
            <artifactId>apache-mailet-api</artifactId>
            <version>${mailet.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
    </dependencies>


</project>

拦截匹配器

Matcher.java

package com.sangbo.mail;

import javax.mail.MessagingException;

import org.apache.james.core.MailAddress;
import org.apache.mailet.base.GenericRecipientMatcher;
/**
 * @author: sangbo
 * @create: 2024-01-11
 * @description: 拦截匹配
 **/
public class Matcher extends GenericRecipientMatcher {
    @Override
    public boolean matchRecipient(MailAddress recipient) throws MessagingException {
        //拦截所有邮件
        return true;
    }
}

拦截处理器

Mailet.java

package com.sangbo.mail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.mail.MessagingException;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
/**
 * @author: sangbo
 * @create: 2024-01-11
 * @description: 拦截器,处理收到的邮件
 **/
public class Mailet extends GenericMailet {
    @Override
    public void service(Mail mail) throws MessagingException {

        //打印邮件内容
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            mail.getMessage().writeTo(byteArrayOutputStream);
            String body = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
            System.out.println(body);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}

导出jar包,导入到james中设置配置信息

导入jar包到james

假设jar包叫 mail-filter.jar
将mail-filter.jar放到james目录下的extensions-jars文件夹中

配置james配置

修改james\conf目录下的mailetcontainer.xml文件
找到processors节点下的processor节点

<processor state="root" enableJmx="true">
    <mailet match="All" class="PostmasterAlias"/>
    <mailet match="RelayLimit=30" class="Null"/>
    <mailet match="All" class="ToProcessor">
        <processor>transport</processor>
    </mailet>
</processor>

添加一行

<mailet match="All" class="com.sangbo.mail.Mailet"/>

最终如下

<processor state="root" enableJmx="true">
    <mailet match="All" class="com.sangbo.mail.Mailet"/>
    <mailet match="All" class="PostmasterAlias"/>
    <mailet match="RelayLimit=30" class="Null"/>
    <mailet match="All" class="ToProcessor">
        <processor>transport</processor>
    </mailet>
</processor>

重启james

提示

在拦截器的代码中
System.out.println(body);
并不会在james的控制台打印日志,如果需要验证,建议通过http请求到后端,或者将内容保存为文件验证。

参考:

https://james.apache.org/server/dev-extend-mailet.html

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值