Program-Me-Rev Program-Me-Rev - 1 year ago 111
Java Question

How to send an email to multiple receipients in Spring

The email gets sents only to the last email address in the

String[] to
array. I'm intending to send to all email addresses added to the array. How can I make that work?

Thank you all in advance.

public void sendMail(String from, String[] to, String subject, String msg, List attachments) throws MessagingException {

// Creating message
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.setText(msg + "<html><body><h1>hi welcome</h1><body></html", true);

Iterator it = attachments.iterator();

while (it.hasNext()) {
FileSystemResource file = new FileSystemResource(new File((String);
helper.addAttachment(file.getFilename(), file);

// Sending message


Answer Source

Use a comma seperated list of addresses:


or use

helper.setTo(StringUtils.join(to,",") );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download