如何将自定义 HeaderFilterStrategy 与 Apache Camel 电子邮件组件结合使用

sam*_*wis 3 java apache-camel

我正在尝试使用驼峰邮件组件通过Amazon Simple Email Service从我的应用程序发送电子邮件。我尝试使用camel-mail组件而不是camel-ses组件的原因是camel-ses不支持附件。

我用它来表示 uri:

smtp://$smtpHost:$smtpPort?username=$smtpUsername&password=$smtpPassword&mail.smtp.ssl.enable=true&mail.smtp.ssl.required=true&mail.smtp.auth=true
Run Code Online (Sandbox Code Playgroud)

但我在运行时收到错误:

com.sun.mail.smtp.SMTPSendFailedException: 554 Transaction failed: Illegal header 'breadcrumbId'.
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2114)
    at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1900)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1122)
    at org.apache.camel.component.mail.DefaultJavaMailSender.send(DefaultJavaMailSender.java:124)
    at org.apache.camel.component.mail.MailProducer.process(MailProducer.java:49)
Run Code Online (Sandbox Code Playgroud)

因此,亚马逊对于可以发送哪些标头似乎非常严格,而 Camels 方法与 SES 的配合不佳:

Camel 将所有交换的 IN 标头复制到 MimeMessage 标头。

深入研究源代码,我可以看到该类org.apache.camel.component.mail.MailBinding具有接受以下类型的构造函数HeaderFilterStrategy

public MailBinding(HeaderFilterStrategy headerFilterStrategy, ContentTypeResolver contentTypeResolver) {
        this.headerFilterStrategy = headerFilterStrategy;
        this.contentTypeResolver = contentTypeResolver;
}
Run Code Online (Sandbox Code Playgroud)

如何配置骆驼邮件以使用自定义HeaderFilterStrategy

Cla*_*sen 5

您可以在端点上配置以使用自定义标头过滤策略。 http://camel.apache.org/how-to-avoid-sending-some-or-all-message-headers.html

您可以使用 # 查找来参考自定义策略:http://camel.apache.org/how-do-i-configure-endpoints.html

然后你可以使用端点 uri,例如

smtp://$smtpHost:$smtpPort?username=$smtpUsername&password=$smtpPassword&mail.smtp.ssl.enable=true&mail.smtp.ssl.required=true&mail.smtp.auth=true&headerFilterStrategy=#myCustomerFilter
Run Code Online (Sandbox Code Playgroud)

其中 myCustomFilter 是您在注册表中登记或在 Spring XML 文件中定义的自定义过滤器的 bean id(如果您使用 spring),例如 < bean id="myCustomFiler">