电子邮件仅会发送到String[] to
数组中的最后一个电子邮件地址。我打算发送到添加到阵列的所有电子邮件地址。我怎样才能做到这一点?
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);
}
答案 0 :(得分:16)
使用以逗号分隔的地址列表:
helper.setTo(to1,to2,to3....);
答案 1 :(得分:6)
更好的方法是创建一个包含多个收件人地址的数组。
function fillStreetLocationSelect() {
$.ajax({
async: true,
url: web + "/FillStreetLocationSelect",
dataType: "json",
method: "POST",
data: JSON.stringify(hazard),
contentType: "application/json; charset=utf-8",
success: function (data) {
/* */
}
});
}
答案 2 :(得分:2)
就这样尝试吧。
helper.setTo(InternetAddress.parse("email1@test.com,email2@test.com"))
答案 3 :(得分:2)
将所有电子邮件ID添加到String []数组中
public String[] sendEmailIds() {
String[] emailIds = new String[4];
emailIds[0] = "abc@mail.com";
emailIds[1] = "deg@mail.com";
emailIds[2] = "sgh@mail.com";
emailIds[3] = "hht@mail.com";
return emailIds;
}
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(sendEmailIds());
mailMessage.setSubject(subject);
mailMessage.setText(message);
mailMessage.setFrom(fromEmailAddress);
javaMailSender.send(mailMessage);
答案 4 :(得分:1)
You can try this, instead of
helper.setTo(to);
String multipleEmailIds = "abc@abc.com, abc@abc.com"
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(multipleEmailIds ));
答案 5 :(得分:0)
我认为更好的方法是在spring.xml文件中将“to”属性声明为数组,传递值并使用Deinum在评论中建议的方法setTo(string[])
。进程在xml文件中将'to'定义为
<property name="to">
<array>
<value>abc@gmail.com</value>
<value>xyz@gmail.com</value>
</array>
</property>
现在为包含多个收件人地址的数组生成getter setter方法,并将其传递给setTo(String[])
方法: -
helper.setTo(to);
答案 6 :(得分:0)
它与SimpleMailMessage配合良好。 像Siri
的答案String[] to = {"user1@gmail.com", "user2@gmail.com"};
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject("subject of mail");
simpleMailMessage.setText("content of mail");
try {
javaMailSender.send(simpleMailMessage);
} catch (MailSendException e) {
//...
}
答案 7 :(得分:-3)
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.ccc.corp"/>
<property name="port" value="25"/>
<property name="javaMailProperties"><props>
<prop key="mail.smtp.sendpartial">true</prop>
</props></property>
</bean>
设置mail.smtp.sendpartial
为真。我相信它的工作对你来说