Page edited by willem jiangMail ComponentThe 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> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> {warning:title=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} {tip:title=POP3 or IMAP} POP3 has some limitations and end users are encouraged to use IMAP if possible. {tip} h3. URI format Mail endpoints can have one of the following URI formats (for the protocols, SMTP, POP3, or IMAP, respectively): smtp://[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 You can append query options to the URI in the following format, {{?option=value&option=value&...}} h4. 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 h4. {anchor:DefaultPorts}Default ports As of Camel 1.4, 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}} | h3. Options || 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.) | | {{defaultEncoding}} | {{null}} | The default encoding to use for Mime Messages. | | {{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}} | {{usern...@host}} | *...@deprecated* Use the {{to}} option instead. The {{TO}} recipients (receivers of the email). | | {{to}} | {{usern...@host}} | As of *Camel 1.4*, the TO recipients (the receivers of the mail). Separate multiple email addresses with a comma. | | {{CC}} | {{null}} | As of *Camel 1.4*, the CC recipients (the receivers of the mail). Separate multiple email addresses with a comma. | | {{BCC}} | {{null}} | As of *Camel 1.4*, the BCC recipients (the receivers of the mail). Separate multiple email addresses with a comma. | | {{from}} | {{ca...@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}}/{{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. As of *Camel 1.5*, the default setting is {{false}}. | | {{delete}} | {{false}} | *Camel 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}}/{{true}} | As of *Camel 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 Camel always skips deleted messages. Setting this option to {{true}} will filter to only unseen messages. As of Camel 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. | | {{unseen}} | {{true}} | *Camel 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 *Camel 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 Camel will not consume any messages at all. | | {{alternateBodyHeader}} | {{mail_alternateBody}} | *Camel 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 Camel 2.0, this option has been renamed to {{alternativeBodyHeader}}. | | {{alternativeBodyHeader}} | {{CamelMailAlternativeBody}} | *Camel 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 *Camel 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 *Camel 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 *Camel 1.4*, the default consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute to avoid overloading the mail server. The default value in Camel 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|http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html] in JDK for details. | | {{mail.XXX}} | {{null}} | As of *Camel 2.0*, you can set any [additional java mail properties|http://java.sun.com/products/javamail/javadocs/index.html]. 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}} | *Camel 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}} | *Camel 2.0:* 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}}. | | {{dummyTrustManager}} | {{false}} | As of *Camel 1.4*, when testing SSL connections, you can enable the dummy trust manager by setting this option to {{true}}. When the dummy trust manager is enabled, the mail client skips the server certificate check. {warning:title=Warning}Enabling this option makes the email connection _completely insecure_. The connection becomes vulnerable to a _man-in-the-middle_ attack, which implies that your login credentials can be stolen. Do not use this option in a production environment.{warning}| | {{ignoreUnsupportedCharset}} | {{false}} | *Camel 2.0:* 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. | h3. 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). h3. Defaults changed in Camel 1.4 As of Camel 1.4 the default consumer delay is now 60 seconds. Camel will therefore only poll the mailbox once a minute to avoid overloading the mail server. The default value in Camel 1.3 is 500 milliseconds. h3. Defaults changed in Camel 1.5 In Camel 1.5 the following default options have 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. h3. Mail Message Content Camel uses the message exchange's IN body as the [MimeMessage|http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html] text content. The body is converted to {{String.class}}. Camel copies all of the exchange's IN headers to the [MimeMessage|http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html] headers. The subject of the [MimeMessage|http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeMessage.html] can be configured using a header property on the IN message. The code below demonstrates this: {snippet:id=e1|lang=java|url="" The same applies for other MimeMessage headers such as recipients, so you can use a header property as {{To}}: {snippet:id=e1|lang=java|url="" h3. Headers take precedence over pre-configured recipients >From Camel 1.5 onwards, 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 {{davscl...@apache.org}}, because it takes precedence over the pre-configured recipient, {{i...@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. {code:java} Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "davscl...@apache.org"); template.sendBodyAndHeaders("smtp://ad...@localhost?to=info@mycompany.com", "Hello World", headers); Multiple recipients for easier configurationAs of Camel 1.5, 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", "davscl...@apache.org ; jstrac...@apache.org ; ningji...@apache.org"); The preceding example uses a semicolon, ;, as the separator character. Setting sender name and emailYou 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", "Claus Ibsen <davscl...@apache.org>"); map.put("From", "James Strachan <jstrac...@apache.org>"); map.put("Subject", "Camel is cool"); SUN JavaMailSUN JavaMail is used under the hood for consuming and producing mails.
SamplesWe 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://ad...@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://ad...@mymailserver.com?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: ca...@riders.org , e...@riders.org // CC: m...@you.org // BCC: some...@somewhere.org String recipients = "&to=ca...@riders.org,e...@riders.org&cc...@you.org&bcc=some...@somewhere.org"; from("direct:a").to("smtp://y...@mymailserver.com?password=secret&from=...@apache.org" + recipients); Sending mail with attachment sample
The mail component supports attachments, which is a feature that was introduced in Camel 1.4. 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://ja...@mymailserver.com?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); SSL sampleIn this sample, we want to poll our Google mail inbox for mails. To download mail onto a local mail client, Google mail requires you to enable and configure SSL. This is done by logging into your Google mail account and changing your settings to allow IMAP access. Google have extensive documentation on how to do this. from("imaps://imap.gmail.com?username=your_usern...@gmail.com&password=YOUR_PASSWORD" + "&deleteProcessedMessages=false&processOnlyUnseenMessages=true&consumer.delay=60000").to("log:newmail"); The preceding route polls the Google mail inbox for new mails once every minute and logs the received messages to the newmail logger category. 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 <0...@mi5.co.uk>], to=your_usern...@gmail.com], subject=[... 2008-05-08 06:32:12,187 INFO newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <0...@mi5.co.uk>], to=your_usern...@gmail.com], subject=[... Consuming mails with attachment sampleIn this sample we poll a mailbox and store all attachments from the mails as files. First, we define a 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://imap.gmail.com?username=your_usern...@gmail.com&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 (attachments.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 = "" class="code-object">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. See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence
- [CONF] Apache Camel > Mail confluence