Wednesday 15 August 2012

java - How to send an email to multiple receipients in Spring -



java - How to send an email to multiple receipients in Spring -

the email gets sents lastly email address in string[] to array. i'm intending send email addresses added array. how can create work?

thank in advance.

public void sendmail(string from, string[] to, string subject, string msg, list attachments) throws messagingexception { // creating message sender.sethost("smtp.gmail.com"); mimemessage mimemsg = sender.createmimemessage(); mimemessagehelper helper = new mimemessagehelper(mimemsg, true); properties props = new properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "425"); session session = session.getdefaultinstance(props, null); helper.setfrom(from); helper.setto(to); helper.setsubject(subject); helper.settext(msg + "<html><body><h1>hi welcome</h1><body></html", true); iterator = attachments.iterator(); while (it.hasnext()) { filesystemresource file = new filesystemresource(new file((string) it.next())); helper.addattachment(file.getfilename(), file); } // sending message sender.send(mimemsg); }

use comma seperated list of addresses:

helper.setto(to1,to2,to3....);

or utilize

helper.setto(stringutils.join(to,",") );

java spring email

No comments:

Post a Comment