【问题标题】:Send email to myself using SMTP and Apache Camel使用 SMTP 和 Apache Camel 向自己发送电子邮件
【发布时间】:2014-06-11 11:17:58
【问题描述】:

我正在尝试根据本教程的第 4 部分编写一个用于向自己发送电子邮件的 Apache Camel 路由:

https://camel.apache.org/tutorial-example-reportincident.html

from("file://target/subfolder")
.setHeader("subject", constant("new incident reported"))
.convertBodyTo(String.class)
// send the email
.to("smtp://myID@localhost?password=&to=myname@mycompany.com");

但我收到了这个,但我的收件箱中没有电子邮件:

395  [main] DEBUG org.apache.camel.example.reportincident.
ReportIncidentRoutesTest  - Routing Rules are: 
[EventDrivenConsumerRoute[Endpoint[direct:start] -> 
Delegate(Delegate(Pipeline[DeadLetterChannel[Delegate(setHeader(org.apache.
camel.file.name, BeanExpression[bean:org.apache.camel.example.reportincident.
FilenameGenerator@244aeb52 method: generateFilename])), 
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]], 
DeadLetterChannel[Delegate(sendTo(Endpoint[velocity:MailBody.vm])), 
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]], 
DeadLetterChannel[Delegate(sendTo(Endpoint[file://target/subfolder])), 
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]]]))], 
EventDrivenConsumerRoute[Endpoint[file://target/subfolder] -> 
Delegate(Delegate(Pipeline[DeadLetterChannel[Delegate(setHeader(To, 
myname@mycompany.com)), RecipientList[log:org.apache.camel.DeadLetterChannel?
level=error]], DeadLetterChannel[Delegate(setHeader(subject, new incident 
reported)), RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]], 
DeadLetterChannel[Delegate(org.apache.camel.processor.
ConvertBodyProcessor@6e79839), 
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]], 
DeadLetterChannel[Delegate(sendTo(Endpoint[smtp://myID@localhost?
password=&to=myname@mycompany.com])),
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]]]))]]

我不知道为什么,也不知道如何解决这个问题。当我运行测试时,我似乎也收到了这些警告:

[WARNING] The POM for com.sun.xml.fastinfoset:FastInfoset:jar:1.2.2 is invalid, 
transitive dependencies (if any) will not be available, 
enable debug logging for more details
[WARNING] The POM for com.sun.xml.bind:jaxb-impl:jar:2.1.7 is invalid, 
transitive dependencies (if any) will not be available, 
enable debug logging for more details
[WARNING] The POM for com.sun.xml.bind:jaxb-xjc:jar:2.1.7 is invalid, 
transitive dependencies (if any) will not be available, 
enable debug logging for more details

...

606  [main] WARN  org.apache.camel.impl.converter.DefaultTypeConverter  - 
Overriding type converter from: StaticMethodTypeConverter: 
public static java.lang.String org.apache.camel.converter.IOConverter.
toString(javax.xml.transform.Source) throws javax.xml.transform.
TransformerException,java.io.IOException to: InstanceMethodTypeConverter: public 
java.lang.String org.apache.camel.converter.jaxp.XmlConverter.toString
(javax.xml.transform.Source) throws javax.xml.transform.TransformerException

【问题讨论】:

  • 你在使用什么类型的文件?
  • 您是否使用任何自定义类型转换器?
  • 警告信息是无害的。您可能需要检查是否有任何其他警告或错误消息。
  • 回答问题:(1) 文件类型为.vm。 (2) 我认为没有任何自定义类型转换器。 (3) 我没有看到任何其他警告或错误消息。
  • 首先,尝试发送常规文本文件而不是 Velocity 模板。或许,Camel 试图施展魔法。

标签: java routing smtp apache-camel


【解决方案1】:

可以忽略 DEBUG 和 WARN 消息。

以下路线定义对我使用 Camel v2.12.3 有效:

from("file://target/subfolder")
    .log("Working on file ${header.CamelFileName}")
    .setHeader("subject", simple("New incident: ${header.CamelFileName}"))
    .to("MY_ID@smtp://localhost?password=MY_PASSWORD&to=myname@mycompany.com"); 

启动路由后,应该会在日志中看到Working on file XXX等消息。

也许问题不是 Camel 路由,而是 localhost 上的 SMTP 服务器。尝试使用另一个电子邮件客户端向您的 SMTP 服务器发送电子邮件,并检查您是否收到任何电子邮件。可以在here 找到如何在 MacOS 上使用 bash shell 来执行此操作的示例。

请检查本地主机上的 SMTP 服务器是否使用默认端口。如果没有,请将端口添加到 URI,例如 localhost:MY_PORT。对于 SMTP,默认值为 25,对于 SMTPS,默认值为 465。可以使用telnet SERVERNAME 25 等telnet 来检查活动端口。

可能不是 SMTP 服务器的问题,而是文件的读取。检查target/subfolder中的文件是否可读且未被Camel锁定,即检查是否没有fileName.camelLock文件。

最后,在扫描完所有文件之前验证您的路线是否继续运行并且不会停止,有关详细信息,请参阅http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html

总结一下我的答案:从小处着手,将大路由分成小路由并分别测试。

编辑: tutorial-example-reportincident 的最新源代码可以在这里找到:https://github.com/apache/camel/tree/master/examples/camel-example-reportincident

【讨论】:

  • 不识别方法 log() 并且 smtp:// 是正确的端点。骆驼版本是 1.5.0
  • @La-comadreja log EIP 是在 Camel v2.2 中引入的(参见 camel.apache.org/logeip.html)。我强烈建议升级到最新版本 2.12.3。
  • 问题是我正在阅读一个用旧版本编写的教程。
  • @La-comadreja 使用更新的教程...?
  • @La-comadreja 您本地的 Maven 可能已损坏。见stackoverflow.com/questions/4856307/…。通过删除本地 repo 中相应的失败下载工件目录来解决此问题。
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 2011-07-30
  • 1970-01-01
相关资源
最近更新 更多