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

Java 使用SMTP和Apache Camel向自己发送电子邮件,java,routing,smtp,apache-camel,Java,Routing,Smtp,Apache Camel,根据本教程的第4部分,我正在尝试编写一个用于向自己发送电子邮件的Apache Camel路由: 但我收到了这个,我的收件箱里没有电子邮件: 395 [main] DEBUG org.apache.camel.example.reportincident. ReportIncidentRoutesTest - Routing Rules are: [EventDrivenConsumerRoute[Endpoint[direct:start] -> Delegate(Delegat

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

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

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

调试和警告消息可以忽略

使用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"); 
启动路由后,您应该会在日志中看到一条消息,例如
正在处理文件XXX

问题可能不是驼峰路由,而是本地主机上的SMTP服务器。尝试使用其他电子邮件客户端向SMTP服务器发送电子邮件,并检查是否收到任何电子邮件。可以找到如何在MacOS上使用bash shell实现这一点的示例

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

SMTP服务器可能不是问题所在,而是文件的读取。检查
目标/子文件夹中的文件是否可读且未被Camel锁定,即检查是否没有
文件名.camelLock
文件

最后,在扫描所有文件之前,请验证路由是否保持运行且没有停止,有关详细信息,请参阅

总结一下我的答案:从小处开始,将大路线分成小路线,然后分别进行测试

编辑:
教程示例reportincident的最新源代码可在此处找到:。

您使用的文件类型是什么?您是否使用自定义类型转换器?警告消息是无害的。您需要检查是否有任何其他警告或错误消息。在回答问题时:(1)文件类型为.vm。(2) 我认为没有任何自定义类型转换器。(3) 我没有看到任何其他警告或错误消息。首先,尝试发送常规文本文件,而不是Velocity模板。也许,Camel尝试了一些魔法。不识别方法log(),smtp://是正确的端点。骆驼版本是1.5。0@La-comadreja在Camel v2.2中引入了
log
EIP(参见)。我强烈建议升级到最新版本2.12.3。问题是我正在学习一个用旧版本编写的教程。@La comadreja使用一个更新的教程…?@La comadreja您的本地专家可能会崩溃。看见通过删除本地repo中相应的失败下载工件目录来解决此问题。
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");