The mail component provides access to Email via Spring's Mail support and the underlying JavaMail system.
This mail component uses Spring Mail Support for sending and receiving mails. If you have trouble with sending mails that they don't contain any subject or has wrong recipients then you might need to declare a MailSender bean in your spring configuration such as:
This has been reported in CAMEL-869 and verified that it works flawless declaring this bean. |
As of Camel 1.4 the default consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute to not exhaust the mail server. The default value in Camel 1.3 is 500 millis. |
smtp://[user-info@]host:port[?password=somepwd] pop3://[user-info@]host:port[?password=somepwd] imap://[user-info@]host:port[?password=somepwd] |
which supports either POP, IMAP or SMTP underlying protocols.
It is possible to omit the user-info and specify the username as a URI parameter instead
smtp://host:port?password=somepwd&username=someuser |
Such as:
smtp://mycompany.mailserver:30?password=tiger&username=scott |
Camel have support for secure mail protocols. Just add a s to the scheme such as:
smtps://[user-info@]host:port[?password=somepwd] pop3s://[user-info@]host:port[?password=somepwd] imaps://[user-info@]host:port[?password=somepwd] |
Its the underlying mail framework that is handling the SSL support. Camel uses SUN JavaMail. However SUN JavaMail only trusts certificates issued by well known Certificate Authorities. So if you have issued your own certificate you have to import it into the local Java keystore file (see SSLNOTES.txt in JavaMail for details). If you are using your own signed certificates sometimes it can be cumbersome to install your certificate in the local keystore. Camel provides a test option dummyTrustManager that when enabled will accept any given certificate. |
As of Camel 1.4 support for default port number has been added. If the port number is omitted Camel will determine based on the protocol the port number to use.
Protocol |
Default Port Number |
---|---|
SMPT |
25 |
SMPTS |
465 |
POP3 |
110 |
POP3S |
995 |
IMAP |
143 |
IMAPS |
993 |
Property |
Default |
Description |
---|---|---|
host |
|
The host name or IP address to connect to |
port |
See Default Ports |
The TCP port number to connect on |
user-info |
|
The user name on the email server |
username |
|
The user name on the email server configured as a URI parameter |
password |
null |
The users password to use, can be omitted if the mail server does not require a password |
ignoreUriScheme |
false |
If enabled Camel will use the scheme to determine the transport protocol to use (pop, imap, smtp etc.) |
defaultEncoding |
null |
The default encoding to use for MineMessages |
contentType |
text/plain |
New option in Camel 1.5. The mail message content type. Use |
folderName |
INBOX |
The folder to poll |
destination |
user-info@host |
@deprecated use To option. The TO recipients (the receivers of the mail) |
to |
user-info@host |
The TO recipients (the receivers of the mail). This option is introduced in Camel 1.4. |
CC |
null |
The CC recipients (the receivers of the mail). This option is introduced in Camel 1.4. |
BCC |
null |
The BCC recipients (the receivers of the mail). This option is introduced in Camel 1.4. |
from |
camel@localhost |
The FROM email address |
deleteProcessedMessages |
true/false |
Deletes the messages after they have been processed. This is done by setting the DELETED flag on the mail message. If false then the flag SEEN is set instead. As of Camel 1.5 the default setting is now false. |
processOnlyUnseenMessages |
false/true |
As of Camel 1.4 its possible to configure MailConsumer to only process unseen messages (eg new messages) or all. Note Camel will always skip deleted messages. Setting this option to true will filter to only unseen messages. As of Camel 1.5 the default setting is now true. |
fetchSize |
-1 |
As of Camel 1.4 a maximum number of messages to consume during a polling can be set. This can be used to not exhaust a mail server if 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 case where Camel will not consume any messages at all. |
debugMode |
false |
As of Camel 1.4 its possible to enable the debug mode on the underlying mail framework. SUN Mail framework will default output to System.out. |
connectionTimeout |
30000 |
As of Camel 1.4 the connection timeout can be configured in millis. Default is 30 seconds. |
dummyTrustManager |
false |
As of Camel 1.4 testing SSL connections can be easier if enabling a dummy TrustManager that trust any given certificate. Notice this is only to be used for testing, as it does not provide any security at all. |
consumer.initialDelay |
1000 |
Millis before the polling starts |
consumer.delay |
60000 |
As of Camel 1.4 the default consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute to not exhaust the mail server. The default value in Camel 1.3 is 500 millis. |
consumer.useFixedDelay |
false |
true to use fixed delay between pools, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details. |
In Camel 1.5 the following default options has changed:
deleteProcessedMessages
is now false as we felt Camel should not delete mails on the mail server by default.processOnlyUnseenMessages
is now true as we felt Camel should only poll new mails by default.Camel will use the Exchange Message IN body as the MimeMessage text content. The body is converted to String.class.
Camel copies all the Exchange Message IN headers to the MimeMessage headers.
The subject of the MimeMessage can be configured using a header property on the in message. The code below demonstrates this:
{snippet:id=e1|lang=java|url=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java} |
The same applies for other MimeMessage headers such as recipients, so you can use a header property as the TO:
{snippet:id=e1|lang=java|url=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailUsingHeadersTest.java} |
From Camel 1.5 onwards the recipients from the message headers will always take precedence over any pre configured. The idea is that if you provide any recipients in the message headers then its what you get (WYSIWYG). The pre configuration is just there for fallback or if you use fixed recipients.
In the sample code below the mail is sent to davsclaus@apache.org
since it will take precedence over the pre configured. Even if we have CC pre configured they will not recieve the mail. The headers is all or nothing, it will not mix and match between headers and pre configured. You either get one or the other.
Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "davsclaus@apache.org"); template.sendBodyAndHeaders("smtp://admin@localhost?to=info@mycompany.com", "Hello World", headers); |
Also new in Camel 1.5 is the possibility to set multiple recipients in a single String parameter. This applied to both the headers and pre configuration.
Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "davsclaus@apache.org ; jstrachan@apache.org ; ningjiang@apache.org"); |
In the sample above we use semi colon as separators. Camel support both semicolon = ;
and comma = ,
as separator char.
We start with a simple route that sends the messages received from a JMS queue as emails. The email account with be the admin account on mymailserver.com.
from("jms://queue:subscription").to("smtp://admin@mymailserver.com?password=secret"); |
In the next sample we will poll a mailbox for new emails once every minute. Notice that we use the special consumer parameter for setting the poll interval consumer.delay as 60000 millis = 60 seconds.
from("imap://admin@mymailserver.com?password=secret&processOnlyUnseenMessages=true&consumer.delay=60000").to("seda://mails"); |
In this sample we want to send a mail to multiple recipients. This feature was introduced in camel 1.4.
{snippet:id=e1|lang=java|url=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java} |
Attachments is a new feature in Camel 1.4 that of course is also supported by the mail component. In the sample below we send a mail message containing a plain text message with a logo file attachment.
{snippet:id=e1|lang=java|url=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java} |
In this sample we want to poll our Google mail inbox for mails. Google mail requires to use SSL and have it configured for other clients to access your mailbox. This is done by logging into your google mail and change your settings to allow IMAP access. Google have extensive documentation how to do this.
from("imaps://imap.gmail.com?username=YOUR_USERNAME@gmail.com&password=YOUR_PASSWORD" + "&deleteProcessedMessages=false&processOnlyUnseenMessages=true&consumer.delay=60000").to("log:newmail"); |
The route above will poll the google mail inbox for new mails once every minute and log it to the newmail logger category.
Running the sample with DEBUG logging enabled we can monitor the progress in the logs:
2008-05-08 06:32:09,640 DEBUG MailConsumer - Connecting to MailStore imaps//imap.gmail.com:993 (SSL enabled), folder=INBOX 2008-05-08 06:32:11,203 DEBUG MailConsumer - Polling mailfolder: imaps//imap.gmail.com:993 (SSL enabled), folder=INBOX 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 <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[... 2008-05-08 06:32:12,187 INFO newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[... |
In the next sample we want to sent mails from Camel using our own mail server using secure connections. As our own mail server is using our own signed certificate we need either to
In the sample we use the dummyTrustManager option:
from("seda:mailsToSend").to("imaps://ourmailsserver.com?username=camelmail&password=secret&dummyTrustManager=true"); |