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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23298249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:44:30  来源:igfitidea点击:

Send email to myself using SMTP and Apache Camel

javaroutingsmtpapache-camel

提问by La-comadreja

I am trying to write an Apache Camel route for sending email to myself, based on Part 4 of this tutorial:

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

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

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=&[email protected]");

But I'm getting this, and no email in my inbox:

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

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, 
[email protected])), 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=&[email protected]])),
RecipientList[log:org.apache.camel.DeadLetterChannel?level=error]]]))]]

I'm not sure why, or how I can fix this problem. I also seem to be getting these warnings when I run the test:

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

[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

回答by Peter Keller

The DEBUG and WARN messages can just be ignored.

DEBUG 和 WARN 消息可以被忽略。

Following route definition worked for me using Camel v2.12.3:

以下路线定义使用 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&[email protected]"); 

After starting the route, you should see a message such as Working on file XXXin the log.

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

Perhaps not the Camel routing is the problem but the SMTP server on localhost. Try to send an email to your SMTP server using another email client and check if you receive any email. An example how to do this using a bash shell on MacOS can be found here.

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

Please, check if your SMTP server on localhost uses the default port. If not, add the port to the URI such as localhost:MY_PORT. For SMTP the default is 25, for SMTPS this is 465. The active port can be checked using telnet such as telnet SERVERNAME 25.

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

The SMTP server may not be the problem but the reading of the files. Check if the files in the target/subfolderare readable and not locked by Camel, i.e. check if there is no fileName.camelLockfile.

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

Finally, verify that your route keeps running and doesn't stop before all your files are scanned, see http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.htmlfor more information about that.

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

To summarize my answer: Start small, split large routes into small ones and test them separately.

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

EDIT:The most recent source code of tutorial-example-reportincident can be found here: https://github.com/apache/camel/tree/master/examples/camel-example-reportincident.

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