[CONF] Apache Camel: Mail (page edited)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[CONF] Apache Camel: Mail (page edited)

Dhiraj Bokde (Confluence)

Mail has been edited by Claus Ibsen (Sep 24, 2008).

Change summary:


(View changes)


Mail Component

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:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="YOUR_MAIL_SERVER_HERE"/>

This has been reported in CAMEL-869 and verified that it works flawless declaring this bean.

Default consumer delay changed

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.

URI format


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


Such as:


SSL support

Camel have support for secure mail protocols. Just add a s to the scheme such as:

SSL Information

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.
Notice: this is strongly discouraged not using in production environments.

Default Ports

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
POP3 110
POP3S 995
IMAP 143


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 text/html for html mails.
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.

Defaults changed in Camel 1.5

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.

Mail Message Content

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:

from("direct:a").setHeader("subject", constant(subject)).to("smtp://james2@localhost");

The same applies for other MimeMessage headers such as recipients, so you can use a header property as the TO:

Map map = new HashMap();
map.put("To", "[hidden email]");
map.put("From", "[hidden email]");
map.put("Subject", "Camel rocks");

String body = "Hello Claus.\nYes it does.\n\nRegards James.";
template.sendBodyAndHeaders("smtp://[hidden email]", body, map);

Headers take precedence over pre configured recipeients

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 [hidden email] 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", "[hidden email]");

        template.sendBodyAndHeaders("smtp://admin@localhost?to=[hidden email]", "Hello World", headers);

Multiple recipients easier configuration

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", "[hidden email] ; [hidden email] ; [hidden email]");

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://[hidden email]?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://[hidden email]?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.

// all the recipients of this mail are:
// To: [hidden email] , [hidden email]
// CC: [hidden email]
// BCC: [hidden email]
String recipients = "&To=[hidden email],[hidden email]&CC=[hidden email]&BCC=[hidden email]";

from("direct:a").to("smtp://[hidden email]?password=secret&From=[hidden email]" + recipients);

Attachment Sample

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.

// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://[hidden email]?password=secret");

// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));

// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
// and let it go (processes the exchange by sending the email)

SSL Sample

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=[hidden email]&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 <[hidden email]>], to=[hidden email]], subject=[... 
2008-05-08 06:32:12,187 INFO  newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <[hidden email]>], to=[hidden email]], subject=[...

SSL Sample with dummyTrustManager

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

  1. install our certificate in the local keystore Camel uses
  2. use the dummyTrustManager option for testing purpose to see if the secure communication works

In the sample we use the dummyTrustManager option:


See Also