Mail ComponentThe mail component provides access to Email via Spring's Mail support and the underlying JavaMail system.
URI formatsmtp://[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
SSL supportCamel 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]
Default PortsAs 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.
Options
Mail Message ContentCamel will use the Exchange Message IN body as the MimeMessage Camel copies all the Exchange Message IN headers to the MimeMessage The subject of the MimeMessage 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", "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); Headers take precedence over pre configured recipeientsFrom 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 davsclaus@apache.org 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", "davsclaus@apache.org"); template.sendBodyAndHeaders("smtp://admin@localhost?To=info@mycompany.com", "Hello World", headers); Multiple recipients easier configurationAlso new in Camel 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", "davsclaus@apache.org ; jstrachan@apache.org ; ningjiang@apache.org"); In the sample above we use semi colon as separators. Camel support both semicolon = ; and comma = , as separator char. SamplesWe 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://admin@mymailserver.com?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://admin@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: 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); Attachment SampleAttachments 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://james@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. 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://imap.gmail.com?username=YOUR_USERNAME@gmail.com&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. 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 <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[... 2008-05-08 06:32:12,187 INFO newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[... SSL Sample with dummyTrustManagerIn the next sample we want to sent mails from Camel using our own mail server using secure connections. As our own mail server is using our own signed certificate we need either to
In the sample we use the dummyTrustManager option: from("seda:mailsToSend").to("imaps://ourmailsserver.com?username=camelmail&password=secret&dummyTrustManager=true"); See Also |