如何将邮件发送给多个收件人
How can I send mail to more than one recipient
我有一个 HTML 表单,用户可以在其中输入多个邮件 ID,但我不知道如何向多个人发送邮件
我成功地向一个用户发送了邮件,但是我在这里发送了多封邮件。
我做了什么:
这是我的 EmailUntility
class:
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();
当我在控制台上打印 recipient
时,我从 UI 获取邮件 ID,因为 abc@gmail.com,efg@gmail.com,123@gmail.com
所有三个都带有 ,
分隔符
当只有一个收件人时,这个工作正常,但当有多个收件人时,我不知道该怎么做
我正在使用 java.mail
api 发送邮件。
这里 toAddress
是由 ,
分隔的电子邮件 ID 组成的字符串
if (toAddress!= null) {
List<String> emails = new ArrayList<>();
if (toAddress.contains(",")) {
emails.addAll(Arrays.asList(toAddress.split(",")));
} else {
emails.add(toAddress);
}
Address[] to = new Address[emails.size()];
int counter = 0;
for(String email : emails) {
to[counter] = new InternetAddress(email.trim());
counter++;
}
message.setRecipients(Message.RecipientType.TO, to);
}
根据您的描述,我假设参数 email-ids
可以有多个值。因此 String recipient = request.getParameter("email-ids");
是错误的。
我将引用 Javadoc on ServletRequest.getParamter(String)
(我强调):
You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues
.
所以应该是String[] recipients = request.getParameterValues("email-ids");
。 (您 可以 也可以尝试使用代码拆分您获得的单个字符串,但如果您已经获得多个值,那么再次连接和拆分它们只会让人感觉错误且有风险。)
使用这些单独的字符串,为您已经在使用的数组 InternetAddress[] toAddresses
创建多个元素应该没有问题。
我有一个 HTML 表单,用户可以在其中输入多个邮件 ID,但我不知道如何向多个人发送邮件
我成功地向一个用户发送了邮件,但是我在这里发送了多封邮件。
我做了什么:
这是我的 EmailUntility
class:
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();
当我在控制台上打印 recipient
时,我从 UI 获取邮件 ID,因为 abc@gmail.com,efg@gmail.com,123@gmail.com
所有三个都带有 ,
分隔符
当只有一个收件人时,这个工作正常,但当有多个收件人时,我不知道该怎么做
我正在使用 java.mail
api 发送邮件。
这里 toAddress
是由 ,
if (toAddress!= null) {
List<String> emails = new ArrayList<>();
if (toAddress.contains(",")) {
emails.addAll(Arrays.asList(toAddress.split(",")));
} else {
emails.add(toAddress);
}
Address[] to = new Address[emails.size()];
int counter = 0;
for(String email : emails) {
to[counter] = new InternetAddress(email.trim());
counter++;
}
message.setRecipients(Message.RecipientType.TO, to);
}
根据您的描述,我假设参数 email-ids
可以有多个值。因此 String recipient = request.getParameter("email-ids");
是错误的。
我将引用 Javadoc on ServletRequest.getParamter(String)
(我强调):
You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use
getParameterValues
.
所以应该是String[] recipients = request.getParameterValues("email-ids");
。 (您 可以 也可以尝试使用代码拆分您获得的单个字符串,但如果您已经获得多个值,那么再次连接和拆分它们只会让人感觉错误且有风险。)
使用这些单独的字符串,为您已经在使用的数组 InternetAddress[] toAddresses
创建多个元素应该没有问题。