|
Mail has been edited by Claus Ibsen (Oct 26, 2008). Change summary: added stuff about SUN API (thanks Dave) Mail ComponentThe mail component provides access to Email via Spring's Mail support and the underlying JavaMail system.
URI formatsmtp://[EMAIL PROTECTED]:port[?password=somepwd] pop3://[EMAIL PROTECTED]:port[?password=somepwd] imap://[EMAIL PROTECTED]: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
Defaults changed in Camel 1.4As 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. Defaults changed in Camel 1.5In Camel 1.5 the following default options has changed:
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://[EMAIL PROTECTED]"); 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); 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 [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://[EMAIL PROTECTED]@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", "[EMAIL PROTECTED] ; [EMAIL PROTECTED] ; [EMAIL PROTECTED]"); In the sample above we use semi colon as separators. Camel support both semicolon = ; and comma = , as separator char. SUN JavaMailSUN JavaMail
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://[EMAIL PROTECTED]");
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]&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: [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]&[EMAIL PROTECTED]" + 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://[EMAIL PROTECTED]"); // 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://[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. 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=[... 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 |
Unsubscribe or edit your notifications preferences
