You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by vcheruvu <v_...@hotmail.com> on 2012/03/30 04:00:44 UTC

ProducerTemplate for sending mails with attachements

Hi,

 I am using Camel-mail component to send emails out to clients for given
byteOutputStream (content is html email) and imagesData in byte[] (image
used in html content). ProducerTemplate.sendBodyandHeaders()  has all the
mail paramter details to send to client. However, I am not able to find a
way where I can add image to the header which will ensure html email content
can embedd the image when sending to clients. Normally, it will work with 
exchange.getIn().addAttchement("cid:001", dataHandler(image name)). But with
ProducerTemplate I do not have way to add the attachments to 
Exchange.getIn() object. 
Even the camel-mail test classes use Exchange.getIn().addAttachement(..) and
use the exchange object in ProducerTemplate. In my cases I do not create
exchange object at all when using ProducerTemplate. 
Could you please advise how I can add attachments through ProducerTemplate? 


public class EmailSender{

       @Autowired
	private ProducerTemplate template;

	private void sendEmail(ByteArrayOutputStream byteArrayOutputStream
			,HashMap<String,byte[]> imagesData, String to, String from) {
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("To", to);
			map.put("From", from);
			map.put("Subject", "Camel rocks");
			// ByteArrayDataSource bs = new
ByteArrayDataSource(imagesData.get("px"),"application/octet-stream");
			//map.put("cid:px", new DataHandler(bs));
		
template.sendBodyAndHeaders("smtp://testServer@host.mailbank?&password=test123&contentType=text/html", 
									byteArrayOutputStream.toString(), map);
		}catch (Exception e){
			LOG.error("Unable to send mail !!!" + e);
		}
		
	}

}


--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-for-sending-mails-with-attachements-tp5605511p5605511.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: ProducerTemplate for sending mails with attachements

Posted by vcheruvu <v_...@hotmail.com>.
Thanks Ashwin and Claus for valuable suggestions. I have applied your
suggestion using Processor and Exchange.  


template.send(smtp://testServer@host.mailbank?&password=test123&contentType=text/html",ExchangePattern.InOnly,
new EmailProcessor(to, from, byteArrayOutputStream, imagesData));

EmailProcessor implements Processor interface. I have added all the
necessary implementation i.e add headers and attachements to the exchange
object. Camel rocks !!...




--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-for-sending-mails-with-attachements-tp5605511p5611283.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: ProducerTemplate for sending mails with attachements

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Mar 30, 2012 at 2:56 PM, Ashwin Karpe <ak...@fusesource.com> wrote:
> Hi,
>
> In order to do this, you need to send an Exchange instead of body and
> headers...
>
> Please check out the code below...
>
> Cheers,
>
> Ashwin...
>
> =======================================================
>        Exchange exchange =
> context.getEndpoint("direct:exception").createExchange(ExchangePattern.InOnly);
>        exchange.getIn().setBody("Hello World");
>        exchange.getIn().addAttachment("foo", new DataHandler(....));
>
>        Exchange out = template.send("direct:exception", exchange);
>
> =======================================================
>

There is also a method on ProducerTemplate that takes a processor. You
can then use a new Processor as inlined, and then set the data on the
exchange as shown be Aswhin above.



> -----
> ---------------------------------------------------------
> Ashwin Karpe
> Apache Camel Committer & Sr Principal Consultant
> FUSESource (a Progress Software Corporation subsidiary)
> http://fusesource.com
>
> Blog: http://opensourceknowledge.blogspot.com
> ---------------------------------------------------------
> --
> View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-for-sending-mails-with-attachements-tp5605511p5606572.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: ProducerTemplate for sending mails with attachements

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

In order to do this, you need to send an Exchange instead of body and
headers...

Please check out the code below...

Cheers,

Ashwin...

=======================================================
        Exchange exchange =
context.getEndpoint("direct:exception").createExchange(ExchangePattern.InOnly);
        exchange.getIn().setBody("Hello World");
        exchange.getIn().addAttachment("foo", new DataHandler(....));

        Exchange out = template.send("direct:exception", exchange);

=======================================================

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/ProducerTemplate-for-sending-mails-with-attachements-tp5605511p5606572.html
Sent from the Camel - Users mailing list archive at Nabble.com.