LibraryToggle FramesPrintFeedback

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

[Warning]Geronimo mail .jar

We have discovered that the geronimo mail .jar (v1.6) has a bug when polling mails with attachments. It cannot correctly identify the Content-Type. So, if you attach a .jpeg file to a mail and you poll it, the Content-Type is resolved as text/plain and not as image/jpeg. For that reason, we have added an org.apache.camel.component.ContentTypeResolver SPI interface which enables you to provide your own implementation and fix this bug by returning the correct Mime type based on the file name. So if the file name ends with jpeg/jpg, you can return image/jpeg.

You can set your custom resolver on the MailComponent instance or on the MailEndpoint instance. This feature is added in Camel 1.6.2/2.0.

[Warning]Geronimo mail .jar

We have discovered that the geronimo mail .jar (v1.6) has a bug when polling mails with attachments. It cannot correctly identify the Content-Type. So, if you attach a .jpeg file to a mail and you poll it, the Content-Type is resolved as text/plain and not as image/jpeg. For that reason, we have added an org.apache.camel.component.ContentTypeResolver SPI interface which enables you to provide your own implementation and fix this bug by returning the correct Mime type based on the file name. So if the file name ends with jpeg/jpg, you can return image/jpeg.

You can set your custom resolver on the MailComponent instance or on the MailEndpoint instance. This feature is added in Fuse Mediation Router 1.6.2/2.0.

[Tip]POP3 or IMAP

POP3 has some limitations and end users are encouraged to use IMAP if possible.

[Important]Using mock-mail for testing

You can use a mock framework for unit testing, which allows you to test without the need for a real mail server. However you should remember to not include the mock-mail when you go into production or other environments where you need to send mails to a real mail server. Just the presence of the mock-javamail.jar on the classpath means that it will kick in and avoid sending the mails.

Property Default Description
host The host name or IP address to connect to.
port 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, Fuse Mediation Router uses the scheme to determine the transport protocol (POP, IMAP, SMTP etc.)
defaultEncoding null The default encoding to use for Mime Messages.
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 username@host @deprecated Use the to option instead. The TO recipients (receivers of the email).
to username@host As of Fuse Mediation Router 1.4, the TO recipients (the receivers of the mail). Separate multiple email addresses with a comma.
CC null As of Fuse Mediation Router 1.4, the CC recipients (the receivers of the mail). Separate multiple email addresses with a comma.
BCC null As of Fuse Mediation Router 1.4, the BCC recipients (the receivers of the mail). Separate multiple email addresses with a comma.
from camel@localhost The FROM email address.
subject As of Camel 2.3, the Subject of the message being sent. Note: Setting the subject in the header takes precedence over this option.
deleteProcessedMessages true 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. As of Fuse Mediation Router 1.5, the default setting is false. This option is named delete in Camel 2.0 onwards.
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, the SEEN flag is set instead.
processOnlyUnseenMessages false As of Fuse Mediation Router 1.4, it is possible to configure a consumer endpoint so that it processes only unseen messages (that is, new messages) or all messages. Note that Fuse Mediation Router always skips deleted messages. Setting this option to true will filter to only unseen messages. As of Fuse Mediation Router 1.5, the default setting is true. POP3 does not support the SEEN flag, so this option is not supported in POP3; use IMAP instead. This option is named unseen in Camel 2.0 onwards.
unseen true Fuse Mediation Router 2.0: Is used to fetch only unseen messages (that is, new messages). Note that POP3 does not support the SEEN flag; use IMAP instead.
fetchSize -1 As of Fuse Mediation Router 1.4, 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 Fuse Mediation Router will not consume any messages at all.
alternateBodyHeader mail_alternateBody Fuse Mediation Router 1.6.1: 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. In Fuse Mediation Router 2.0, this option has been renamed to alternativeBodyHeader.
alternativeBodyHeader CamelMailAlternativeBody Fuse Mediation Router 2.0: 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 As of Fuse Mediation Router 1.4, 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 As of Fuse Mediation Router 1.4, the connection timeout can be configured in milliseconds. Default is 30 seconds.
consumer.initialDelay 1000 Milliseconds 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 avoid overloading the mail server. The default value in Fuse Mediation Router 1.3 is 500 milliseconds.
consumer.useFixedDelay false Set to true to use a fixed delay between polls, 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, for example: mail.pop3.forgettopheaders=true&mail.mime.encodefilename=true.
maxMessagesPerPoll 0 Fuse Mediation Router 2.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 e.g. 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 Fuse Mediation Router 2.0: Specifies a pluggable org.springframework.mail.javamail.JavaMailSender instance in order to use a custom email implementation. If none provided, Fuse Mediation Router uses the default, org.springframework.mail.javamail.JavaMailSenderImpl.
ignoreUnsupportedCharset false Fuse Mediation Router 2.0: Option to let Fuse Mediation Router 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.

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

Fuse Mediation Router 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://james2@localhost");

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

Map map = new HashMap();
map.put("To", "davsclaus@apache.org");
map.put("From", "jstrachan@apache.org");
map.put("Subject", "Camel rocks");

String body = "Hello Claus.\nYes it does.\n\nRegards James.";
template.sendBodyAndHeaders("smtp://davsclaus@apache.org", body, map);

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 in Implementing Routes:

Map headers = new HashMap();
map.put("To", "Claus Ibsen <davsclaus@apache.org>");
map.put("From", "James Strachan <jstrachan@apache.org>");
map.put("Subject", "Camel is cool");

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.

Comments powered by Disqus