Camel Component: Mail - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

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

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-mail</artifactId>
   <!-- use the same version as your Camel core version -->
   <version>x.x.x</version>
</dependency>

URI format and options

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

smtp://[username@]host[:port][?options]
pop3://[username@]host[:port][?options]
imap://[username@]host[:port][?options]

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:

smtps://[username@]host[:port][?options]
pop3s://[username@]host[:port][?options]
imaps://[username@]host[:port][?options]

You can append query options to the URI in the following format, ?option=value&option=value&..., where option can be:

Table 16. 

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

null

The password on the email server.

ignoreUriScheme

false

If false, Camel uses the scheme to determine the transport protocol (POP, IMAP, SMTP etc.)

contentType

text/plain

The mail message content type. Use text/html for 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

The TO recipients (the receivers of the mail). Separate multiple email addresses with a comma.

replyTo

alias@host

The Reply-To recipients (the receivers of the response mail). Separate multiple email addresses with a comma.

CC

null

The CC recipients (the receivers of the mail). Separate multiple email addresses with a comma.

BCC

null

The BCC recipients (the receivers of the mail). Separate multiple email addresses with a comma.

from

camel@localhost

The FROM email address.

subject

 

The Subject of the message being sent. Note: Setting the subject in the header takes precedence over this option.

peek

 true

Camel 2.11.3/2.12.2: Consumer only. Will mark the javax.mail.Message as peeked before processing the mail message. This applies to IMAPMessage messages types only. By using peek the mail will not be eager marked as SEEN on the mail server, which allows us to rollback the mail message if there is an error processing in Camel.

delete

false

Deletes the messages after they have been processed. This is done by setting the DELETED flag on the mail message. If false, the SEEN flag is set instead. You can override this configuration option by setting a header with the key delete to specify whether the mail should be deleted.

unseen

true

Is used to fetch only unseen messages (that is, new messages). Note that POP3 does not support the SEEN flag; use IMAP instead. Important: This option is not in use if you also use searchTerm options. Instead if you want to disable unseen when using searchTerm's then add searchTerm.unseen=false as a term.

copyTo

null

Consumer only. After processing a mail message, it can be copied to a mail folder with the given name. You can override this configuration value with a header with the key copyTo, allowing you to copy messages to folder names configured at runtime.

fetchSize

-1

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 -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.

alternativeBody-Header

CamelMailAlternat-iveBody

Specifies the key to an IN message header that contains an alternative email body. For example, if you send emails in text/html 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.

debugMode

false

It is possible to enable debug mode on the underlying mail framework. The SUN Mail framework logs the debug messages to System.out by default.

connectionTimeout

30000

The connection timeout can be configured in milliseconds. Default is 30 seconds.

consumer. initialDelay

1000

Milliseconds before the polling starts.

consumer.delay

60000

The default consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute to avoid overloading the mail server.

consumer. useFixedDelay

false

Set to true to use a fixed delay between polls, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details.

disconnect

false

Whether the consumer should disconnect after polling. If enabled this forces Camel to connect on each poll.

closeFolder

true

Whether the consumer should close the folder after polling. Setting this option to false while having disconnect=false will require the consumer to keep the folder open between polls.

mail.XXX

null

You can set any additional java mail properties. 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: mail.pop3.forgettopheaders=true. You can set multiple such options, for example: mail.pop3.forgettopheaders=true& mail.mime.encodefilename=true.

mapMailMessage

true

Specifies whether Camel should map the received mail message to Camel body/headers. If set to true, the body of the mail message is mapped to the body of the Camel IN message and the mail headers are mapped to IN headers. If this option is set to false then the IN message contains a raw javax.mail.Message. You can retrieve this raw message by calling exchange.getIn().getBody(javax.mail. Message.class).

maxMessagesPerPoll

0

Specifies the maximum number of messages to gather per poll. By default, no maximum is set. Can be used to set a limit of, for example, 1000 to avoid downloading thousands of files when the server starts up. Set a value of 0 or negative to disable this option.

javaMailSender

null

Specifies a pluggable org.springframework.mail.javamail. JavaMailSender instance in order to use a custom email implementation. If none provided, Camel uses the default, org.springframework.mail.javamail. JavaMailSenderImpl.

ignoreUnsupported-Charset

false

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 default instead.

sslContext-Parameters

null

Reference to a org. apache. camel. util. jsse. SSLContextParameters in the Registry. This reference overrides any configured SSLContextParameters at the component level. See Using the JSSE Configuration Utility for more information.

searchTerm

null

Starting with Camel 2.11, refers to a SearchTerm which allows for filtering mails based on search criteria such as subject, body, from, sent after a certain date, etc.

searchTerm.xxx

null

Starting with Camel 2.11, to configure search terms directly from the endpoint URI, which supports a limited number of terms defined by the SimpleSearchTerm class.


Sample endpoints

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

smtp://[username@]host[:port][?password=somepwd]

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

smtp://host[:port]?password=somepwd&username=someuser

For example:

smtp://mycompany.mailserver:30?password=tiger&username=scott

Default ports

Default port numbers are supported. If the port number is omitted, Camel determines the port number to use based on the protocol.

Protocol

Default Port Number

SMTP

25

SMTPS

465

POP3

110

POP3S

995

IMAP

143

IMAPS

993

SSL support

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 SSLNOTES.txt in JavaMail for details).

Mail Message Content

Camel uses the message exchange's IN body as the MimeMessage text content. The body is converted to String.class.

Camel copies all of the exchange's 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://joe2@localhost");

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

Map<String, Object> map = new HashMap<String, Object>();
map.put("To", "jenshansen@gmail.com");
map.put("From", "jbloggs@gmail.com");
map.put("Subject", "Camel rocks");

String body = "Hello Jens.\nYes it does.\n\nRegards Joe.";
template.sendBodyAndHeaders("smtp://jenshansen@gmail.com", body, map);

Starting with Camel 2.11, when using the MailProducer to send the mail to the server, you should be able to get the message id of the MimeMessage using the CamelMailMessageId key from the Camel message header.

Headers take precedence over pre-configured recipients

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.

In the sample code below, the email message is sent to jenshansen@gmail.com, 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.

Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("to", "jenshansen@gmail.com");

template.sendBodyAndHeaders(
   "smtp://admin@localhost?to=info@mycompany.com", 
   "Hello World", headers);

Multiple recipients for easier configuration

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:

Map<String, Object> headers = new HashMap<String, Object>();
headers.put("to", "jenshansen@gmail.com ; jbloggs@gmail.com ; janedoe@gmail.com");

The preceding example uses a semicolon, ;, as the separator character.

Setting sender name and email

You can specify recipients in the format, name <email>, to include both the name and the email address of the recipient.

For example, you define the following headers on the a Message :

Map headers = new HashMap();
map.put("To", "Jens Hansen <jenshansen@gmail.com>");
map.put("From", "Joe Bloggs <jbloggs@gmail.com>");
map.put("Subject", "Camel is cool");

SUN JavaMail

SUN JavaMail is used under the hood for consuming and producing mails. 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, so end users are recommended to use IMAP where possible.

Samples

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

from("jms://queue:subscription")
   .to("smtp://admin@mymailserver.com?password=secret");

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

from("imap://admin@mymailserver.com&password=secret
&unseen=true&consumer.delay=60000")
   .to("seda://mails");

In this sample we want to send a mail to multiple recipients.

// all the recipients of this mail are:
// To: camel@riders.org , easy@riders.org
// CC: me@you.org
// BCC: someone@somewhere.org
String recipients = "&To=camel@riders.org,easy@riders.org&
CC=me@you.org&BCC=someone@somewhere.org";

from("direct:a")
   .to("smtp://you@mymailserver.com?password=secret&From=you@apache.org" 
+ recipients);

Check the Apache Camel website for several more examples, including handling mail attachments and SSL configuration.