我有一个HTML表单,用户可以在其中输入多个邮件ID,但我不知道如何将邮件发送给多个人
我成功地向一个用户发送邮件,但在这里我被困在发送多封电子邮件中。
我做了什么:
这是我的类:EmailUntility
public class EmailUtility {
public static void sendEmail(String host, String port, final String userName, final String password,
String toAddress, String subject, String message) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(false);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport.send(msg);
}
}
这个是我的Servlet doPost
String recipient = request.getParameter("email-ids");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
System.out.println(recipient);
try {
EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
content);
} catch (Exception ex) {
ex.printStackTrace();
当我在控制台上打印时,我从UI获取邮件ID,因为所有这三个都带有分离器recipientabc@gmail.com,efg@gmail.com,123@gmail.com,
当只有一个收件人时,这个工作正常,但是当有多个收件人时,我不知道该怎么做
我正在使用 API 发送邮件。java.mail
慕桂英4014372
宝慕林4294392
慕运维8079593
相关分类