camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Mail
Date Mon, 29 Mar 2010 01:09:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Mail">Mail</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Mail-MailComponent"></a>Mail Component</h2>

<p>The mail component provides access to Email via Spring's Mail support and the underlying
JavaMail system.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-mail&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;

{warning:title=Geronimo mail .jar}
We have discovered that the geronimo mail {{.jar}} (v1.6) has a bug when polling mails with
attachments. It cannot correctly identify the {{Content-Type}}. So, <span class="code-keyword">if</span>
you attach a {{.jpeg}} file to a mail and you poll it, the {{Content-Type}} is resolved as
{{text/plain}} and not as {{image/jpeg}}. For that reason, we have added an {{org.apache.camel.component.ContentTypeResolver}}
SPI <span class="code-keyword">interface</span> which enables you to provide your
own implementation and fix <span class="code-keyword">this</span> bug by returning
the correct Mime type based on the file name. So <span class="code-keyword">if</span>
the file name ends with {{jpeg/jpg}}, you can <span class="code-keyword">return</span>
{{image/jpeg}}.

You can set your custom resolver on the {{MailComponent}} instance or on the {{MailEndpoint}}
instance. This feature is added in Camel 1.6.2/2.0.
{warning}

{tip:title=POP3 or IMAP}
POP3 has some limitations and end users are encouraged to use IMAP <span class="code-keyword">if</span>
possible.
{tip}

h3. URI format

Mail endpoints can have one of the following URI formats (<span class="code-keyword">for</span>
the protocols, SMTP, POP3, or IMAP, respectively):

</pre>
</div></div>
<p>smtp://<span class="error">&#91;username@&#93;</span>host<a
href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a><br/>
pop3://<span class="error">&#91;username@&#93;</span>host<a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a><br/>
imap://<span class="error">&#91;username@&#93;</span>host<a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

The mail component also supports secure variants of these protocols (layered over SSL). You
can enable the secure protocols by adding {{s}} to the scheme:

</pre>
</div></div>
<p>smtps://<span class="error">&#91;username@&#93;</span>host<a
href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a><br/>
pop3s://<span class="error">&#91;username@&#93;</span>host<a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a><br/>
imaps://<span class="error">&#91;username@&#93;</span>host<a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Foptions&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;options</a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

You can append query options to the URI in the following format, {{?option=value&amp;option=value&amp;...}}

h4. Sample endpoints

Typically, you specify a URI with login credentials as follows (taking SMTP as an example):

</pre>
</div></div>
<p>smtp://<span class="error">&#91;username@&#93;</span>host<a
href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=%3Fpassword%3Dsomepwd&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">&#63;password=somepwd</a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

Alternatively, it is possible to specify both the user name and the password as query options:

</pre>
</div></div>
<p>smtp://host<a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=port&amp;linkCreation=true&amp;fromPageId=52932"
class="createlink">:port</a>?password=somepwd&amp;username=someuser</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

For example:
</pre>
</div></div>
<p>smtp://mycompany.mailserver:30?password=tiger&amp;username=scott</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

h4. {anchor:DefaultPorts}Default ports
As of Camel 1.4, <span class="code-keyword">default</span> port numbers are supported.
If the port number is omitted, Camel determines the port number to use based on the protocol.

|| Protocol || Default Port <span class="code-object">Number</span> ||
| {{SMTP}} | {{25}} |
| {{SMTPS}} | {{465}} |
| {{POP3}} | {{110}} |
| {{POP3S}} | {{995}} |
| {{IMAP}} | {{143}} |
| {{IMAPS}} | {{993}} |

h3. Options

|| Property || Default || Description ||
| {{host}} | | The host name or IP address to connect to. |
| {{port}} | See [#DefaultPorts] | The TCP port number to connect on. |
| {{username}} | | The user name on the email server. |
| {{password}} | {{<span class="code-keyword">null</span>}} | The password on
the email server. |
| {{ignoreUriScheme}} | {{<span class="code-keyword">false</span>}} | If {{<span
class="code-keyword">false</span>}}, Camel uses the scheme to determine the transport
protocol (POP, IMAP, SMTP etc.) |
| {{defaultEncoding}} | {{<span class="code-keyword">null</span>}} | The <span
class="code-keyword">default</span> encoding to use <span class="code-keyword">for</span>
Mime Messages. |
| {{contentType}} | {{text/plain}} | New option in *Camel 1.5*. The mail message content type.
Use {{text/html}} <span class="code-keyword">for</span> HTML mails. |
| {{folderName}} | {{INBOX}} | The folder to poll. |
| {{destination}} | {{username@host}} | *@deprecated* Use the {{to}} option instead. The {{TO}}
recipients (receivers of the email). |
| {{to}} | {{username@host}} | As of *Camel 1.4*, the TO recipients (the receivers of the
mail). Separate multiple email addresses with a comma. |
| {{CC}} | {{<span class="code-keyword">null</span>}} | As of *Camel 1.4*, the
CC recipients (the receivers of the mail). Separate multiple email addresses with a comma.
|
| {{BCC}} | {{<span class="code-keyword">null</span>}} | As of *Camel 1.4*, the
BCC recipients (the receivers of the mail). Separate multiple email addresses with a comma.
|
| {{from}} | {{camel@localhost}} | The FROM email address. |
| {{subject}} | | As of *Camel 2.3*, the Subject of the message being sent. Note: Setting
the subject in the header takes precedence over <span class="code-keyword">this</span>
option. |
| {{deleteProcessedMessages}} | {{<span class="code-keyword">true</span>}}/{{<span
class="code-keyword">false</span>}} | Deletes the messages after they have been processed.
This is done by setting the {{DELETED}} flag on the mail message. If {{<span class="code-keyword">false</span>}},
the {{SEEN}} flag is set instead. As of *Camel 1.5*, the <span class="code-keyword">default</span>
setting is {{<span class="code-keyword">false</span>}}. |
| {{delete}} | {{<span class="code-keyword">false</span>}} | *Camel 2.0*: Deletes
the messages after they have been processed. This is done by setting the {{DELETED}} flag
on the mail message. If {{<span class="code-keyword">false</span>}}, the {{SEEN}}
flag is set instead. |
| {{processOnlyUnseenMessages}} | {{<span class="code-keyword">false</span>}}/{{<span
class="code-keyword">true</span>}} | As of *Camel 1.4*, it is possible to configure
a consumer endpoint so that it processes only unseen messages (that is, <span class="code-keyword">new</span>
messages) or all messages. Note that Camel always skips deleted messages. Setting <span
class="code-keyword">this</span> option to {{<span class="code-keyword">true</span>}}
will filter to only unseen messages. As of Camel 1.5, the <span class="code-keyword">default</span>
setting is {{<span class="code-keyword">true</span>}}. POP3 does not support the
{{SEEN}} flag, so <span class="code-keyword">this</span> option is not supported
in POP3; use IMAP instead. |
| {{unseen}} | {{<span class="code-keyword">true</span>}} | *Camel 2.0*: Is used
to fetch only unseen messages (that is, <span class="code-keyword">new</span>
messages). Note that POP3 does not support the {{SEEN}} flag; use IMAP instead. |
| {{fetchSize}} | {{-1}} | As of *Camel 1.4*, <span class="code-keyword">this</span>
option sets the maximum number of messages to consume during a poll. This can be used to avoid
overloading a mail server, <span class="code-keyword">if</span> a mailbox folder
contains a lot of messages. Default value of {{-1}} means no fetch size and all messages will
be consumed. Setting the value to 0 is a special corner <span class="code-keyword">case</span>,
where Camel will not consume any messages at all. |
| {{alternateBodyHeader}} | {{mail_alternateBody}} | *Camel 1.6.1*: Specifies the key to an
IN message header that contains an alternative email body. For example, <span class="code-keyword">if</span>
you send emails in {{text/html}} format and want to provide an alternative mail body <span
class="code-keyword">for</span> non-HTML email clients, set the alternative mail
body with <span class="code-keyword">this</span> key as a header. In Camel 2.0,
<span class="code-keyword">this</span> option has been renamed to {{alternativeBodyHeader}}.
|
| {{alternativeBodyHeader}} | {{CamelMailAlternativeBody}} | *Camel 2.0*: Specifies the key
to an IN message header that contains an alternative email body. For example, <span class="code-keyword">if</span>
you send emails in {{text/html}} format and want to provide an alternative mail body <span
class="code-keyword">for</span> non-HTML email clients, set the alternative mail
body with <span class="code-keyword">this</span> key as a header. |
| {{debugMode}} | {{<span class="code-keyword">false</span>}} | As of *Camel 1.4*,
it is possible to enable debug mode on the underlying mail framework. The SUN Mail framework
logs the debug messages to {{<span class="code-object">System</span>.out}} by
<span class="code-keyword">default</span>. |
| {{connectionTimeout}} | {{30000}} | As of *Camel 1.4*, the connection timeout can be configured
in milliseconds. Default is 30 seconds. |
| {{consumer.initialDelay}} | {{1000}} | Milliseconds before the polling starts. |
| {{consumer.delay}} | {{60000}} | As of *Camel 1.4*, the <span class="code-keyword">default</span>
consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute
to avoid overloading the mail server. The <span class="code-keyword">default</span>
value in Camel 1.3 is 500 milliseconds. |
| {{consumer.useFixedDelay}} | {{<span class="code-keyword">false</span>}} | Set
to {{<span class="code-keyword">true</span>}} to use a fixed delay between polls,
otherwise fixed rate is used. See [ScheduledExecutorService|http:<span class="code-comment">//java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html]
in JDK <span class="code-keyword">for</span> details. |
</span>| {{mail.XXX}} | {{<span class="code-keyword">null</span>}} | As
of *Camel 2.0*, you can set any [additional java mail properties|http:<span class="code-comment">//java.sun.com/products/javamail/javadocs/index.html].
For instance <span class="code-keyword">if</span> you want to set a special property
when using POP3 you can now provide the option directly in the URI such as: {{mail.pop3.forgettopheaders=<span
class="code-keyword">true</span>}}. You can set multiple such options, <span class="code-keyword">for</span>
example: {{mail.pop3.forgettopheaders=<span class="code-keyword">true</span>&amp;mail.mime.encodefilename=<span
class="code-keyword">true</span>}}. |
</span>| {{maxMessagesPerPoll}} | {{0}} | *Camel 2.0:* Specifies the maximum number
of messages to gather per poll. By <span class="code-keyword">default</span>,
no maximum is set. Can be used to set a limit of e.g. 1000 to avoid downloading thousands
of files when the server starts up. Set a value of 0 or negative to disable <span class="code-keyword">this</span>
option. |
| {{javaMailSender}} | {{<span class="code-keyword">null</span>}} | *Camel 2.0:*
Specifies a pluggable {{org.springframework.mail.javamail.JavaMailSender}} instance in order
to use a custom email implementation. If none provided, Camel uses the <span class="code-keyword">default</span>,
{{org.springframework.mail.javamail.JavaMailSenderImpl}}. |
| {{dummyTrustManager}} | {{<span class="code-keyword">false</span>}} | As of
*Camel 1.4*, when testing SSL connections, you can enable the dummy trust manager by setting
<span class="code-keyword">this</span> option to {{<span class="code-keyword">true</span>}}.
When the dummy trust manager is enabled, the mail client skips the server certificate check.
{warning:title=Warning}Enabling <span class="code-keyword">this</span> option
makes the email connection _completely insecure_. The connection becomes vulnerable to a _man-in-the-middle_
attack, which implies that your login credentials can be stolen. Do not use <span class="code-keyword">this</span>
option in a production environment.{warning}|
| {{ignoreUnsupportedCharset}} | {{<span class="code-keyword">false</span>}} |
*Camel 2.0:* Option to let Camel ignore unsupported charset in the local JVM when sending
mails. If the charset is unsupported then {{charset=XXX}} (where {{XXX}} represents the unsupported
charset) is removed from the {{content-type}} and it relies on the platform <span class="code-keyword">default</span>
instead. | 

h3. SSL support
The underlying mail framework is responsible <span class="code-keyword">for</span>
providing SSL support. Camel uses SUN JavaMail, which only trusts certificates issued by well
known Certificate Authorities. So <span class="code-keyword">if</span> you issue
your own certificate, you have to <span class="code-keyword">import</span> it
into the local Java keystore file (see {{SSLNOTES.txt}} in JavaMail <span class="code-keyword">for</span>
details).

h3. Defaults changed in Camel 1.4
As of Camel 1.4 the <span class="code-keyword">default</span> consumer delay is
now 60 seconds. Camel will therefore only poll the mailbox once a minute to avoid overloading
the mail server. The <span class="code-keyword">default</span> value in Camel
1.3 is 500 milliseconds.

h3. Defaults changed in Camel 1.5
In Camel 1.5 the following <span class="code-keyword">default</span> options have
changed:
- {{deleteProcessedMessages}} is now *<span class="code-keyword">false</span>*,
as we felt Camel should not delete mails on the mail server by <span class="code-keyword">default</span>.
- {{processOnlyUnseenMessages}} is now *<span class="code-keyword">true</span>*,
as we felt Camel should only poll <span class="code-keyword">new</span> mails
by <span class="code-keyword">default</span>.

h3. Mail Message Content
Camel uses the message exchange's IN body as the [MimeMessage|http:<span class="code-comment">//java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html]
text content. The body is converted to {{<span class="code-object">String</span>.class}}.
</span>
Camel copies all of the exchange's IN headers to the [MimeMessage|http:<span class="code-comment">//java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html]
headers. 
</span>
The subject of the [MimeMessage|http:<span class="code-comment">//java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html]
can be configured using a header property on the IN message. The code below demonstrates <span
class="code-keyword">this</span>:
</span>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java}

The same applies <span class="code-keyword">for</span> other MimeMessage headers
such as recipients, so you can use a header property as {{To}}:
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailUsingHeadersTest.java}

h3. Headers take precedence over pre-configured recipients
>From Camel 1.5 onwards, the recipients specified in the message headers always take precedence
over recipients pre-configured in the endpoint URI. The idea is that <span class="code-keyword">if</span>
you provide any recipients in the message headers, that is what you get. The recipients pre-configured
in the endpoint URI are treated as a fallback.

In the sample code below, the email message is sent to {{davsclaus@apache.org}}, because it
takes precedence over the pre-configured recipient, {{info@mycompany.com}}. Any {{CC}} and
{{BCC}} settings in the endpoint URI are also ignored and those recipients will not receive
any mail. The choice between headers and pre-configured settings is all or nothing: the mail
component _either_ takes the recipients exclusively from the headers or exclusively from the
pre-configured settings. It is not possible to mix and match headers and pre-configured settings.

{code:java}
        Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;
headers = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
<span class="code-object">Object</span>&gt;();
        headers.put(<span class="code-quote">"to"</span>, <span class="code-quote">"davsclaus@apache.org"</span>);

        template.sendBodyAndHeaders(<span class="code-quote">"smtp:<span class="code-comment">//admin@localhost?to=info@mycompany.com"</span>,
<span class="code-quote">"Hello World"</span>, headers);</span>
</pre>
</div></div>

<h3><a name="Mail-Multiplerecipientsforeasierconfiguration"></a>Multiple
recipients for easier configuration</h3>
<p>As of Camel 1.5, it is possible to set multiple recipients using a comma-separated
or a semicolon-separated list. This applies both to header settings and to settings in an
endpoint URI. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        Map&lt;<span class="code-object">String</span>, <span class="code-object">Object</span>&gt;
headers = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
<span class="code-object">Object</span>&gt;();
        headers.put(<span class="code-quote">"to"</span>, <span class="code-quote">"davsclaus@apache.org
; jstrachan@apache.org ; ningjiang@apache.org"</span>);
</pre>
</div></div>

<p>The preceding example uses a semicolon, <tt>;</tt>, as the separator
character.</p>

<h3><a name="Mail-Settingsendernameandemail"></a>Setting sender name and
email</h3>
<p>You can specify recipients in the format, <tt>name &lt;email&gt;</tt>,
to include both the name and the email address of the recipient.</p>

<p>For example, you define the following headers on the a <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Map headers = <span class="code-keyword">new</span> HashMap();
map.put(<span class="code-quote">"To"</span>, <span class="code-quote">"Claus
Ibsen &lt;davsclaus@apache.org&gt;"</span>);
map.put(<span class="code-quote">"From"</span>, <span class="code-quote">"James
Strachan &lt;jstrachan@apache.org&gt;"</span>);
map.put(<span class="code-quote">"Subject"</span>, <span class="code-quote">"Camel
is cool"</span>);
</pre>
</div></div>

<h3><a name="Mail-SUNJavaMail"></a>SUN JavaMail </h3>
<p><a href="http://java.sun.com/products/javamail/" rel="nofollow">SUN JavaMail</a>
is used under the hood for consuming and producing mails. <br/>
We encourage end-users to consult these references when using either POP3 or IMAP protocol.
Note particularly that POP3 has a much more limited set of features than IMAP.</p>
<ul class="alternate" type="square">
	<li><a href="http://java.sun.com/products/javamail/javadocs/com/sun/mail/pop3/package-summary.html"
rel="nofollow">SUN POP3 API</a></li>
	<li><a href="http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html"
rel="nofollow">SUN IMAP API</a></li>
	<li>And generally about the <a href="http://java.sun.com/products/javamail/javadocs/javax/mail/Flags.html"
rel="nofollow">MAIL Flags</a></li>
</ul>


<h3><a name="Mail-Samples"></a>Samples</h3>

<p>We start with a simple route that sends the messages received from a JMS queue as
emails. The email account is the <tt>admin</tt> account on <tt>mymailserver.com</tt>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"jms:<span class="code-comment">//queue:subscription"</span>).to(<span
class="code-quote">"smtp://admin@mymailserver.com?password=secret"</span>);</span>
</pre>
</div></div>

<p>In the next sample, we poll a mailbox for new emails once every minute. Notice that
we use the special <tt>consumer</tt> option for setting the poll interval, <tt>consumer.delay</tt>,
as 60000 milliseconds = 60 seconds.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"imap:<span class="code-comment">//admin@mymailserver.com?password=secret&amp;processOnlyUnseenMessages=<span
class="code-keyword">true</span>&amp;consumer.delay=60000"</span>).to(<span
class="code-quote">"seda://mails"</span>);</span>
</pre>
</div></div>

<p>In this sample we want to send a mail to multiple recipients. This feature was introduced
in camel 1.4:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// all the recipients of <span
class="code-keyword">this</span> mail are:
</span><span class="code-comment">// To: camel@riders.org , easy@riders.org
</span><span class="code-comment">// CC: me@you.org
</span><span class="code-comment">// BCC: someone@somewhere.org
</span><span class="code-object">String</span> recipients = <span class="code-quote">"&amp;To=camel@riders.org,easy@riders.org&amp;CC=me@you.org&amp;BCC=someone@somewhere.org"</span>;

from(<span class="code-quote">"direct:a"</span>).to(<span class="code-quote">"smtp:<span
class="code-comment">//you@mymailserver.com?password=secret&amp;From=you@apache.org"</span>
+ recipients);</span>
</pre>
</div></div>

<h3><a name="Mail-Sendingmailwithattachmentsample"></a>Sending mail with
attachment sample</h3>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Attachments
are not support by all Camel components</b><br /><p>The <em>Attachments
API</em> is based on the Java Activation Framework and is generally only used by the
Mail API. Since many of the other Camel components do not support attachments, the attachments
could potentially be lost as they propagate along the route. The rule of thumb, therefore,
is to add attachments just before sending a message to the mail endpoint. </p></td></tr></table></div>

<p>The mail component supports attachments, which is a feature that was introduced in
Camel 1.4. In the sample below, we send a mail message containing a plain text message with
a logo file attachment.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// create an exchange with a normal body and attachment to
be produced as email
</span>Endpoint endpoint = context.getEndpoint(<span class="code-quote">"smtp:<span
class="code-comment">//james@mymailserver.com?password=secret"</span>);
</span>
<span class="code-comment">// create the exchange with the mail message that is multipart
with a file and a Hello World text/plain message.
</span>Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody(<span class="code-quote">"Hello World"</span>);
in.addAttachment(<span class="code-quote">"logo.jpeg"</span>, <span class="code-keyword">new</span>
DataHandler(<span class="code-keyword">new</span> FileDataSource(<span class="code-quote">"src/test/data/logo.jpeg"</span>)));

<span class="code-comment">// create a producer that can produce the exchange (= send
the mail)
</span>Producer producer = endpoint.createProducer();
<span class="code-comment">// start the producer
</span>producer.start();
<span class="code-comment">// and let it go (processes the exchange by sending the email)
</span>producer.process(exchange);

</pre>
</div></div>

<h3><a name="Mail-SSLsample"></a>SSL sample</h3>

<p>In this sample, we want to poll our Google mail inbox for mails. To download mail
onto a local mail client, Google mail requires you to enable and configure SSL. This is done
by logging into your Google mail account and changing your settings to allow IMAP access.
Google have extensive documentation on how to do this.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"imaps:<span class="code-comment">//imap.gmail.com?username=YOUR_USERNAME@gmail.com&amp;password=YOUR_PASSWORD"</span>
</span>    + <span class="code-quote">"&amp;deleteProcessedMessages=<span
class="code-keyword">false</span>&amp;processOnlyUnseenMessages=<span class="code-keyword">true</span>&amp;consumer.delay=60000"</span>).to(<span
class="code-quote">"log:newmail"</span>);
</pre>
</div></div>

<p>The preceding route polls the Google mail inbox for new mails once every minute and
logs the received messages to the <tt>newmail</tt> logger category.<br/>
Running the sample with <tt>DEBUG</tt> logging enabled, we can monitor the progress
in the logs:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
2008-05-08 06:32:09,640 DEBUG MailConsumer - Connecting to MailStore imaps<span class="code-comment">//imap.gmail.com:993
(SSL enabled), folder=INBOX
</span>2008-05-08 06:32:11,203 DEBUG MailConsumer - Polling mailfolder: imaps<span
class="code-comment">//imap.gmail.com:993 (SSL enabled), folder=INBOX 
</span>2008-05-08 06:32:11,640 DEBUG MailConsumer - Fetching 1 messages. Total 1 messages.

2008-05-08 06:32:12,171 DEBUG MailConsumer - Processing message: messageNumber=[332], from=[James
Bond &lt;007@mi5.co.uk&gt;], to=YOUR_USERNAME@gmail.com], subject=[... 
2008-05-08 06:32:12,187 INFO  newmail - Exchange[MailMessage: messageNumber=[332], from=[James
Bond &lt;007@mi5.co.uk&gt;], to=YOUR_USERNAME@gmail.com], subject=[...
</pre>
</div></div>

<h3><a name="Mail-Consumingmailswithattachmentsample"></a>Consuming mails
with attachment sample</h3>
<p>In this sample we poll a mailbox and store all attachments from the mails as files.
First, we define a route to poll the mailbox. As this sample is based on google mail, it uses
the same route as shown in the SSL sample:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"imaps:<span class="code-comment">//imap.gmail.com?username=YOUR_USERNAME@gmail.com&amp;password=YOUR_PASSWORD"</span>
</span>    + <span class="code-quote">"&amp;deleteProcessedMessages=<span
class="code-keyword">false</span>&amp;processOnlyUnseenMessages=<span class="code-keyword">true</span>&amp;consumer.delay=60000"</span>).process(<span
class="code-keyword">new</span> MyMailProcessor());
</pre>
</div></div>

<p>Instead of logging the mail we use a processor where we can process the mail from
java code:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-keyword">public</span> void process(Exchange exchange) <span
class="code-keyword">throws</span> Exception {
        <span class="code-comment">// the API is a bit clunky so we need to loop
</span>        Map&lt;<span class="code-object">String</span>, DataHandler&gt;
attachments = exchange.getIn().getAttachments();
        <span class="code-keyword">if</span> (attachments.size() &gt; 0) {
            <span class="code-keyword">for</span> (<span class="code-object">String</span>
name : attachments.keySet()) {
                DataHandler dh = attachments.get(name);
                <span class="code-comment">// get the file name
</span>                <span class="code-object">String</span> filename
= dh.getName();

                <span class="code-comment">// get the content and convert it to <span
class="code-object">byte</span>[]
</span>                 <span class="code-object">byte</span>[] data = exchange.getContext().getTypeConverter().convertTo(<span
class="code-object">byte</span>[].class, dh.getInputStream());

                <span class="code-comment">// write the data to a file
</span>                FileOutputStream out = <span class="code-keyword">new</span>
FileOutputStream(filename);
                out.write(data);
                out.flush();
                out.close();
            }
        }
   }
</pre>
</div></div>
<p>As you can see the API to handle attachments is a bit clunky but it's there so you
can get the <tt>javax.activation.DataHandler</tt> so you can handle the attachments
using standard API.        </p>

<h3><a name="Mail-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Mail">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=52932&revisedVersion=72&originalVersion=71">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Mail?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message