Spring Integration 5.5.12添加Disposition-Notification-To头失败求助
在Spring Integration Mail中添加Disposition-Notification-To邮件头的正确方式
你遇到的问题主要源于两个关键错误:XML配置的通道指向错误,以及自定义MIME头未通过正确方式注入,以下是针对两种方法的修正方案:
一、修正XML配置方法
你的XML配置存在两个核心问题:
- 邮件出站适配器指向
enrichedEmailChannel,但添加Disposition-Notification-To头的enricher输出是notificationAddedEmailChannel,导致消息根本没经过该enricher; - 使用普通的
<int:header-enricher>添加头,这类头属于Spring Message内部元数据,不会自动转换为邮件MIME头,需用<int-mail:header-enricher>处理邮件专属MIME头。
修正后的XML配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-mail="http://www.springframework.org/schema/integration/mail" xmlns:util="http://www.springframework.org/schema/util"> <int:channel id="sendEmailChannel" /> <int:channel id="enrichedEmailChannel" /> <int:channel id="notificationAddedEmailChannel" /> <int:gateway id="emailGateway" service-interface="mydomain.EmailGateway" default-request-channel="sendEmailChannel" /> <int:transformer ref="emailInvoiceTransformer" method="transformEmailMessageToMessage" input-channel="sendEmailChannel" output-channel="transformedEmailChannel" /> <int-mail:header-enricher input-channel="transformedEmailChannel" output-channel="enrichedEmailChannel"> <int-mail:content-type value="text/html; charset=utf-8" /> </int-mail:header-enricher> <!-- 使用int-mail专属的header-enricher添加MIME头 --> <int-mail:header-enricher input-channel="enrichedEmailChannel" output-channel="notificationAddedEmailChannel"> <int-mail:header name="Disposition-Notification-To" value="me@mydomain.com" /> </int-mail:header-enricher> <!-- 出站适配器指向经过MIME头处理的通道 --> <int-mail:outbound-channel-adapter channel="notificationAddedEmailChannel" host="${mail.host}" port="${mail.port}" /> </beans>
二、修正MessageBuilder代码方法
直接通过setHeader添加的自定义头属于Spring Message内部元数据,不会自动映射为邮件MIME头。需将自定义MIME头放入MailHeaders.MIME_HEADERS这个专用Map类型头中,Spring Integration Mail会自动将其转换为邮件的MIME头。
修正后的代码:
import org.springframework.integration.mail.MailHeaders; import java.util.HashMap; import java.util.Map; // ...省略其他代码 Map<String, String> mimeHeaders = new HashMap<>(); mimeHeaders.put("Disposition-Notification-To", emailAddressReplyTo); Message<?> message = MessageBuilder.withPayload(payload) .setHeader("mail_to", emailAddressTo) .setHeader("mail_from", emailAddressReplyTo) .setHeader("mail_reply_to", emailAddressReplyTo) .setHeader("mail_subject", "TEST EMAIL") // 将自定义MIME头放入专用的MIME_HEADERS映射中 .setHeader(MailHeaders.MIME_HEADERS, mimeHeaders) .build(); return message;
另外,若直接构造MimeMessage作为消息体,也可直接调用MIME API添加头:
MimeMessage mimeMessage = // 构造你的MIME消息 mimeMessage.addHeader("Disposition-Notification-To", emailAddressReplyTo); Message<?> message = MessageBuilder.withPayload(mimeMessage).build();
内容的提问来源于stack exchange,提问作者Tommy
相关产品推荐
相关产品推荐

