The mail component provides access to Email via Spring's Mail support and the underlying JavaMail system.
![]() | Classpath issue |
---|---|
If you have trouble with sending mails that for instance doesn't contain any
subject, has wrong recipients or other unforeseen errors then it could be because of
having |
![]() | POP3 or IMAP |
---|---|
POP3 has some limitations and end users is encouraged to use IMAP if possible. |
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
FUSE Mediation Router 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]
![]() | SSL Information |
---|---|
Its the underlying mail framework that is handling the SSL support. FUSE Mediation Router 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. FUSE Mediation Router 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 FUSE Mediation Router 1.4 support for default port number has been added. If the port number is omitted FUSE Mediation Router 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 disabled FUSE Mediation Router 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 FUSE Mediation Router 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 FUSE Mediation Router 1.4. |
CC | null | The CC recipients (the receivers of the mail). This option is introduced in FUSE Mediation Router 1.4. |
BCC | null | The BCC recipients (the receivers of the mail). This option is introduced in FUSE Mediation Router 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 FUSE Mediation Router 1.5 the default setting is now false. |
delete | false | FUSE Mediation Router 2.0: 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. |
processOnlyUnseenMessages | false/true | As of FUSE Mediation Router 1.4 its possible to configure MailConsumer to only process unseen messages (eg new messages) or all. Note FUSE Mediation Router will always skip deleted messages. Setting this option to true will filter to only unseen messages. As of FUSE Mediation Router 1.5 the default setting is now true. POP3 does not support the SEEN flag so this option is not supported using POP3, instead use IMAP. |
unseen | true | FUSE Mediation Router 2.0: Is used to only fetch unseen messages (eg new messages). Note: POP3 does not support the SEEN flag, instead use IMAP. |
fetchSize | -1 | As of FUSE Mediation Router 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 FUSE Mediation Router will not consume any messages at all. |
debugMode | false | As of FUSE Mediation Router 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 FUSE Mediation Router 1.4 the connection timeout can be configured in millis. Default is 30 seconds. |
dummyTrustManager | false | As of FUSE Mediation Router 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 FUSE Mediation Router 1.4 the default consumer delay is now 60 seconds. FUSE Mediation Router will therefore only poll the mailbox once a minute to not exhaust the mail server. The default value in FUSE Mediation Router 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. |
mail.XXX | null | As of FUSE Mediation Router 2.0 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, such as:
mail.pop3.forgettopheaders=true&mail.mime.encodefilename=true .
|
As of FUSE Mediation Router 1.4 the default consumer delay is now 60 seconds. FUSE Mediation Router will therefore only poll the mailbox once a minute to not exhaust the mail server. The default value in FUSE Mediation Router 1.3 is 500 millis.
In FUSE Mediation Router 1.5 the following default options has changed:
deleteProcessedMessages
is now false as we felt FUSE Mediation Router should not delete mails
on the mail server by default.
processOnlyUnseenMessages
is now true as we felt FUSE Mediation Router should only poll new mails
by default.
FUSE Mediation Router will use the Exchange Message IN body as the MimeMessage text content. The body is converted to String.class.
FUSE Mediation Router 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", "[email protected]"); map.put("From", "[email protected]"); map.put("Subject", "Camel rocks"); String body = "Hello Claus.\nYes it does.\n\nRegards James."; template.sendBodyAndHeaders("smtp://[email protected]", body, map);
From FUSE Mediation Router 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 [email protected]
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", "[email protected]"); template.sendBodyAndHeaders("smtp://admin@[email protected]", "Hello World", headers);
Also new in FUSE Mediation Router 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", "[email protected] ; [email protected] ; [email protected]");
In the sample above we use semi colon as separators. FUSE Mediation Router support
both semicolon = ;
and comma = ,
as separator
char.
SUN JavaMail is used under the covers for consuming and producing mails. We encourage end-users to consult these links when using either POP3 or IMAP protocol. Notice that especially POP3 have limited features compared to IMAP.
And generally about the MAIL Flags
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://[email protected]?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://[email protected]?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 FUSE Mediation Router 1.4.
// all the recipients of this mail are: // To: [email protected] , [email protected] // CC: [email protected] // BCC: [email protected] String recipients = "&[email protected],[email protected]&[email protected]&[email protected]"; from("direct:a").to("smtp://[email protected]?password=secret&[email protected]" + recipients);
Attachments is a new feature in FUSE Mediation Router 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://[email protected]?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 producer.start(); // and let it go (processes the exchange by sending the email) producer.process(exchange);
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://[email protected]&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 <[email protected]>], [email protected]], subject=[... 2008-05-08 06:32:12,187 INFO newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <[email protected]>], [email protected]], subject=[...
In the next sample we want to sent mails from FUSE Mediation Router using our own mail server using secure connections. As our own mail server is using our own signed certificate we need either to
install our certificate in the local keystore FUSE Mediation Router uses
use the dummyTrustManager option for testing purpose to see if the secure communication works
In the sample we use the dummyTrustManager option:
from("seda:mailsToSend").to("imaps://ourmailsserver.com?username=camelmail&password=secret&dummyTrustManager=true");
In this sample we poll a mailbox and want to store all attachments from the mails as files. First we define our route to poll the mailbox. As this sample is based on google mail, it uses the same route as shown in the SSL sample:
from("imaps://[email protected]&password=YOUR_PASSWORD" + "&deleteProcessedMessages=false&processOnlyUnseenMessages=true&consumer.delay=60000").process(new MyMailProcessor());
Instead of logging the mail we use a processor where we can process the mail from java code:
public void process(Exchange exchange) throws Exception { // the API is a bit clunky so we need to loop Map<String, DataHandler> attachments = exchange.getIn().getAttachments(); if (attacments.size() > 0) { for (String name : attachments.keySet()) { DataHandler dh = attachments.get(name); // get the file name String filename = dh.getName(); // get the content and convert it to byte[] byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, dh.getInputStream()); // write the data to a file FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); } } }
As you can see the API to handle attachments is a bit clunky but it's there so you can
get the javax.activation.DataHandler
so you can handle the
attachments using standard API.