如何在Spring中向多个收件人发送电子邮件 [英] How to send an email to multiple recipients in Spring
本文介绍了如何在Spring中向多个收件人发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
电子邮件仅发送到 String [] to
数组中的最后一个电子邮件地址.我打算发送到添加到阵列的所有电子邮件地址.我该怎么做?
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?
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 it = attachments.iterator();
while (it.hasNext()) {
FileSystemResource file = new FileSystemResource(new File((String) it.next()));
helper.addAttachment(file.getFilename(), file);
}
// Sending message
sender.send(mimeMsg);
}
推荐答案
您可以选择使用以下4种方法.我提供了在这种情况下有用的两种方法的示例.我已经整合了以下评论者提供的信息.
You have the choice to use the following 4 methods. I have provided examples of the two methods useful in this case. I have consolidated this information from the commentators below.
helper.setTo(InternetAddress.parse("email1@test.com,email2@test.com"))
helper.setTo(new String[]{"email1@test.com", "email2@test.com"});
这篇关于如何在Spring中向多个收件人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文