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:
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.
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.
Default Port Number
The host name or IP address to connect to
See Default Ports
The TCP port number to connect on
The user name on the email server
The user name on the email server configured as a URI parameter
The users password to use, can be omitted if the mail server does not require a password
If enabled Camel will use the scheme to determine the transport protocol to use (pop, imap, smtp etc.)
The default encoding to use for MineMessages
New option in Camel 1.5. The mail message content type. Use text/html for html mails.
The folder to poll
@deprecated use To option. The TO recipients (the receivers of the mail)
The TO recipients (the receivers of the mail). This option is introduced in Camel 1.4.
The CC recipients (the receivers of the mail). This option is introduced in Camel 1.4.
The BCC recipients (the receivers of the mail). This option is introduced in Camel 1.4.
The FROM email address
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.
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.
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.
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.
As of Camel 1.4 the connection timeout can be configured in millis. Default is 30 seconds.
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.
Millis before the polling starts
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.
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.
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.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)
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.
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: