使用SMTP和Apache Camel向我自己发送电子邮件

时间:2014-04-25 16:19:25

标签: java routing smtp apache-camel

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

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 个答案:

答案 0 :(得分:2)

可以忽略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执行此操作的示例。

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

)检查活动端口

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