L'e-mail ottiene i messaggi solo all'ultimo indirizzo e-mail nell'array String[] to
. Ho intenzione di inviare a tutti gli indirizzi email aggiunti alla matrice. Come posso fare quel lavoro?Come inviare un'e-mail a più destinatari in primavera
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);
}
Supponendo che il metodo corretto viene richiamato (il 'Setto (String [])' uno) dovrebbe funzionare. Quello che non capisco è il motivo per cui si impostano le proprietà di sessione e l'host in questo metodo, che dovrebbe essere nella configurazione. –