猿问

为什么 Apache Camel 邮件组件将正文作为附件发送?

我正在使用 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 模板的结果直接插入到电子邮件正文中?



守候你守候我
浏览 228回答 2
2回答

千万里不及你

虽然我仍然不太明白这里发生了什么,但我确实有一个可行的解决方案。我可以更改电子邮件的内容类型的唯一方法是在路由到邮件端点之前在 Camel 消息上设置“Content-Type”标头:.setHeader("Content-Type", constant("text/plain"))我什至无法通过使用邮件组件上的“contentType”查询参数来更改内容类型。

当年话下

你最近怎么样,经历了类似的事情并通过以下方式解决了我希望它会有所帮助@Handlerpublic void attachmentValidate(@ExchangeProperty("MAIL_ATTACHMENTS") List<Attachment> attachments,&nbsp; &nbsp; &nbsp; &nbsp; Exchange exchange) throws Exception {&nbsp; &nbsp; Message in = exchange.getIn();&nbsp; &nbsp; if (attachments != null) {&nbsp; &nbsp; &nbsp; &nbsp; for (Attachment attachment : attachments) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileNameMap fileNameMap = URLConnection.getFileNameMap();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String mimeType = fileNameMap.getContentTypeFor(attachment.getName()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .substring(attachment.getName().indexOf('.'), attachment.getName().length()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (StringUtils.isEmpty(mimeType)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mimeType = "application/octet-stream";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] decoded = Base64.getDecoder().decode(attachment.getValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in.addAttachment(attachment.getName(), new DataHandler(new ByteArrayDataSource(decoded, mimeType)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; exchange.setProperty("MAIL_ATTACHMENTS", attachments);}
随时随地看视频慕课网APP

相关分类

Java
我要回答