首页 > 解决方案 > How to write camel blueprint (send email)

问题描述

I'm trying sent mail with apache Camel with xml. So I build postfix , dovecot and servicemix same server. I wrote below xmlfile. but it doesn't work and occur a error

Please teach me how to write xml

<?xml version="1.0" encoding="UTF-8"?>
<blueprint
  xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://www.osgi.org/xmlns/blueprint/v1.0.0
    https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

  <!--  -->
  <camelContext xmlns="http://camel.apache.org/schema/blueprint">

  <!-- -->
  <route>
          <from uri="smtp://localhost?from=testuser01@domain?subject=testmail" />
          <to uri="pop3://localhost?to=testuser02@domain" />
  </route>

  </camelContext>

</blueprint>

You can view the error below:

2018-08-06 00:46:44,007 | ERROR | mix-7.0.0/deploy | BlueprintCamelContext            | 40 - org.apache.camel.camel-blueprint - 2.16.4 | Error occurred during starting Camel: CamelContext(camel-3) due Protocol smtp cannot be used for a MailConsumer. Please use another protocol such as pop3 or imap.

============================================

@Namphibian Thank you for your quick reply. I fixed the code and retlied , but didn't work.

You can view the error below

・ERROR

WARN  |  pop3://IPAddress | MailConsumer                     | 43 - org.apache.camel.camel-core - 2.16.4 | Consumer Consumer[pop3://IPAddress?to=user%40domain] failed polling endpoint: Endpoint[pop3://IPAddress?to=user%40domain]. Will try again at next poll. Caused by: [javax.mail.AuthenticationFailedException - failed to connect, no user name specified?]

I thougut that I need to wirite username and Password, but didn't work

・CODE

<route>                                                                                                         
          <from uri="pop3://IPaddress?to=user@domain?username=user@domain?password=password" />
          <log message="received the message with this content: ${body}" />
          <to uri="smtp://IPaddress?from=admin@domain?subject=testmail" />
  </route>

You can view the error below

WARN  |  pop3://IPAddress | MailConsumer                     | 43 - org.apache.camel.camel-core - 2.16.4 | Consumer Consumer[pop3://IPAddress?to=user%40domain%3Fusername%3Duser%40domain%3Fpassword%3Duser] failed polling endpoint: Endpoint[pop3://IPAddress?to=user%40domain%3Fusername%3Duser%40domain%3Fpassword%3Duser]. Will try again at next poll. Caused by: [javax.mail.AuthenticationFailedException - failed to connect, no user name specified?]

How should I do to send and receive a mail?

============================================

Refixed code and tried again. But didn't work.

・CODE

<route>                                                                                                         
          <from uri="pop3://IPaddress?to=user@domain?username=user@domain&amp;password=password" />
          <log message="received the message with this content: ${body}" />
          <to uri="smtp://IPaddress?from=admin@domain?subject=testmail" />
</route>

・ERROR

WARN  |  pop3://IPAddress | MailConsumer                     | 43 - org.apache.camel.camel-core - 2.16.4 | Consumer Consumer[pop3://IPAddress?password=xxxxxx&to=user%40domain%3Fusername%3Duser%40domain] failed polling endpoint: Endpoint[pop3://IPAddress?password=xxxxxx&to=user%40domain%3Fusername%3Duser%40domain]. Will try again at next poll. Caused by: [javax.mail.AuthenticationFailedException - [AUTH] Authentication failed.]
IPAddress

I thougut %40 represents @. Should I fix some special charactor?

I find URL that wrote about how to escape special character. But couldn't find bout "@". http://camel.apache.org/how-do-i-use-uris-with-parameters-in-xml.html

标签: apache-camelblueprintapache-camel-mail

解决方案


SMTP 用于发送邮件,POP 用于读取邮件。您不能在路由的 from 部分使用 SMTP 的原因是您没有任何东西可以发送 from。

另一点是您说向 POP 组件发送消息。POP 用于阅读邮件。所以你的路线要求做一个读()写组件(SMTP)。

<from uri="smtp://localhost?from=testuser01@domain?subject=testmail" />

然后您要求 write() 一个读取组件 (POP)。

<to uri="pop3://localhost?to=testuser02@domain" />

如果您颠倒组件的顺序,以便您读取(从)读取组件(POP)并写入(到)到写入组件(SMTP),它可能会起作用。

换句话说:

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
      <from uri="pop3://localhost?to=testuser02@domain" /> 
      <log message="received the message with this content: ${body}"/>
      <to uri="smtp://localhost?from=testuser01@domain?subject=testmail" />
 </route>
 </camelContext>

希望这是有道理的。您的组件顺序错误,它们用于不同的目的,因此顺序很重要。


推荐阅读