【问题标题】:Why does the Apache Camel mail component send the body as an attachment?为什么 Apache Camel 邮件组件将正文作为附件发送?
【发布时间】:2019-01-16 03:59:50
【问题描述】:

我正在使用 Apache Camel 2.22.0 并且有一个发送电子邮件的简单路由(带有一些属性引用):

public class EmailFailureRoute extends RouteBuilder {

@Override
public void configure() {
    from("seda:mail")
        .setHeader("To", simple("{{mail.failure.to}}"))
        .setHeader("From", simple("{{mail.failure.from}}"))
        .setHeader("Subject", constant("TEST!"))
        .to("velocity://templates/failure-mail.vm")
        .to("{{mail.smtpServer}}");
  }

}

我期望得到的是一封普通的电子邮件,邮件正文中包含来自 Velocity 模板的文本。我实际上得到的是一封电子邮件,其中包含来自 Velocity 模板的文本附加到电子邮件。在 MS Outlook 中看起来像这样:

为什么是附件?如何让邮件组件将 Velocity 模板的结果直接插入到邮件正文中?

更新:

通过打开邮件组件上的“debugMode”标志,我能够看到发送到 SMTP 服务器的实际电子邮件的内容。看来我的问题是 Content-Type 是'application/json'!因此,现在附加正文是有道理的,但我不知道为什么要以这种方式设置 Content-Type。即使在邮件端点上设置查询参数 'contentType=text/plain' 对电子邮件的最终 Content-Type 也没有影响。

【问题讨论】:

  • 您的邮件是纯文本吗?还是html?尤其是 Outlook 可能对内容很固执。你能把邮件发到一个Gmail帐户并检查一下吗?我不得不通过 Apache camel 邮件组件让 Outlook 正确显示图像。 MS 在 Outlook 中拥有自己的邮件处理方式。
  • 邮件组件默认使用 MIME 类型 text/plain,尽管我尝试过 text/plain 和 text/html 的结果相同。我还尝试将电子邮件发送到 GMail 帐户,但电子邮件的正文中仍然有附件而不是文本。

标签: java apache-camel


【解决方案1】:

虽然我仍然不太了解这里发生了什么,但我确实有一个可行的解决方案。我可以更改电子邮件的内容类型的唯一方法是在路由到邮件端点之前在 Camel 消息上设置“Content-Type”标头:

.setHeader("Content-Type", constant("text/plain"))

我什至无法通过使用邮件组件上的“contentType”查询参数来更改内容类型。

【讨论】:

  • 是的,就像我说的那样,有一些箍要跳过。我必须在邮件(html 正文)中插入图像,并且我必须设置很多额外的东西。事后应该把它写在某个地方。
【解决方案2】:

你最近怎么样,经历过类似的事情,并通过以下方式解决了,希望对你有所帮助

@Handler
public void attachmentValidate(@ExchangeProperty("MAIL_ATTACHMENTS") List<Attachment> attachments,
        Exchange exchange) throws Exception {
    Message in = exchange.getIn();
    if (attachments != null) {
        for (Attachment attachment : attachments) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String mimeType = fileNameMap.getContentTypeFor(attachment.getName()
                    .substring(attachment.getName().indexOf('.'), attachment.getName().length()));
            if (StringUtils.isEmpty(mimeType)) {
                mimeType = "application/octet-stream";
            }
            byte[] decoded = Base64.getDecoder().decode(attachment.getValue());
            in.addAttachment(attachment.getName(), new DataHandler(new ByteArrayDataSource(decoded, mimeType)));
        }
    }
    exchange.setProperty("MAIL_ATTACHMENTS", attachments);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2015-03-27
    • 2014-11-27
    • 2011-03-23
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多