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 Fri, 11 Dec 2009 09: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/~davsclaus">Claus
Ibsen</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>

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

<p>You can set your custom resolver on the <tt>MailComponent</tt> instance
or on the <tt>MailEndpoint</tt> instance. This feature is added in Camel 1.6.2/2.0.</p></td></tr></table></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>POP3 or IMAP</b><br
/><p>POP3 has some limitations and end users are encouraged to use IMAP if possible.</p></td></tr></table></div>

<h3><a name="Mail-URIformat"></a>URI format</h3>

<p>Mail endpoints can have one of the following URI formats (for the protocols, SMTP,
POP3, or IMAP, respectively):</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
smtp:<span class="code-comment">//[username@]host[:port][?options]
</span>pop3:<span class="code-comment">//[username@]host[:port][?options]
</span>imap:<span class="code-comment">//[username@]host[:port][?options]</span>
</pre>
</div></div>

<p>The mail component also supports secure variants of these protocols (layered over
SSL). You can enable the secure protocols by adding <tt>s</tt> to the scheme:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
smtps:<span class="code-comment">//[username@]host[:port][?options]
</span>pop3s:<span class="code-comment">//[username@]host[:port][?options]
</span>imaps:<span class="code-comment">//[username@]host[:port][?options]</span>
</pre>
</div></div>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h4><a name="Mail-Sampleendpoints"></a>Sample endpoints</h4>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
smtp:<span class="code-comment">//[username@]host[:port][?password=somepwd]</span>
</pre>
</div></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
smtp:<span class="code-comment">//host[:port]?password=somepwd&amp;username=someuser</span>
</pre>
</div></div>

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

<h4><a name="Mail-Defaultports"></a><a name="Mail-DefaultPorts"></a>Default
ports</h4>
<p>As of Camel 1.4, default port numbers are supported. If the port number is omitted,
Camel determines the port number to use based on the protocol.</p>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Protocol </th>
<th class='confluenceTh'> Default Port Number </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>SMPT</tt> </td>
<td class='confluenceTd'> <tt>25</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>SMPTS</tt> </td>
<td class='confluenceTd'> <tt>465</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>POP3</tt> </td>
<td class='confluenceTd'> <tt>110</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>POP3S</tt> </td>
<td class='confluenceTd'> <tt>995</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>IMAP</tt> </td>
<td class='confluenceTd'> <tt>143</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>IMAPS</tt> </td>
<td class='confluenceTd'> <tt>993</tt> </td>
</tr>
</tbody></table>

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

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

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

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

<h3><a name="Mail-DefaultschangedinCamel1.5"></a>Defaults changed in Camel
1.5</h3>
<p>In Camel 1.5 the following default options have changed:</p>
<ul class="alternate" type="square">
	<li><tt>deleteProcessedMessages</tt> is now <b>false</b>, as
we felt Camel should not delete mails on the mail server by default.</li>
	<li><tt>processOnlyUnseenMessages</tt> is now <b>true</b>,
as we felt Camel should only poll new mails by default.</li>
</ul>


<h3><a name="Mail-MailMessageContent"></a>Mail Message Content</h3>
<p>Camel uses the message exchange's IN body as the <a href="http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html"
rel="nofollow">MimeMessage</a> text content. The body is converted to <tt>String.class</tt>.</p>

<p>Camel copies all of the exchange's IN headers to the <a href="http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html"
rel="nofollow">MimeMessage</a> headers. </p>

<p>The subject of the <a href="http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html"
rel="nofollow">MimeMessage</a> can be configured using a header property on the IN
message. The code below demonstrates this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:a"</span>).setHeader(<span
class="code-quote">"subject"</span>, constant(subject)).to(<span class="code-quote">"smtp:<span
class="code-comment">//james2@localhost"</span>);</span>
</pre>
</div></div>

<p>The same applies for other MimeMessage headers such as recipients, so you can use
a header property as <tt>To</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Map map = <span class="code-keyword">new</span> HashMap();
map.put(<span class="code-quote">"To"</span>, <span class="code-quote">"davsclaus@apache.org"</span>);
map.put(<span class="code-quote">"From"</span>, <span class="code-quote">"jstrachan@apache.org"</span>);
map.put(<span class="code-quote">"Subject"</span>, <span class="code-quote">"Camel
rocks"</span>);

<span class="code-object">String</span> body = <span class="code-quote">"Hello
Claus.\nYes it does.\n\nRegards James."</span>;
template.sendBodyAndHeaders(<span class="code-quote">"smtp:<span class="code-comment">//davsclaus@apache.org"</span>,
body, map);</span>
</pre>
</div></div>

<h3><a name="Mail-Headerstakeprecedenceoverpreconfiguredrecipients"></a>Headers
take precedence over pre-configured recipients</h3>
<p>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 if 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.</p>

<p>In the sample code below, the email message is sent to <tt>davsclaus@apache.org</tt>,
because it takes precedence over the pre-configured recipient, <tt>info@mycompany.com</tt>.
Any <tt>CC</tt> and <tt>BCC</tt> 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 <em>either</em>
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.</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"</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=67&originalVersion=66">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