亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將郵件發送給多個收件人

如何將郵件發送給多個收件人

ibeautiful 2022-09-07 17:25:04
我有一個HTML表單,用戶可以在其中輸入多個郵件ID,但我不知道如何將郵件發送給多個人我成功地向一個用戶發送郵件,但在這里我被困在發送多封電子郵件中。我做了什么:這是我的類:EmailUntilitypublic 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,因為所有這三個都帶有分離器[email protected],[email protected],[email protected],當只有一個收件人時,這個工作正常,但是當有多個收件人時,我不知道該怎么做我正在使用 API 發送郵件。java.mail
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

這是一個字符串,由以下部分分隔的電子郵件ID組成: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);

}


查看完整回答
反對 回復 2022-09-07
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

根據你的描述,我假設參數可以有多個值。因此是錯誤的。email-idsString recipient = request.getParameter("email-ids");

我將引用ServletRequest.getParamter(String)上的Javadoc(由我強調):

僅當確定參數只有一個值時,才應使用此方法。如果參數可能有多個值,請使用 。getParameterValues

所以它應該是相反的。(您也可以嘗試拆分代碼中獲得的單個字符串,但如果您已經獲得了多個值,那么再次連接并拆分它們只會感覺錯誤且有風險。String[] recipients = request.getParameterValues("email-ids");

有了這些單獨的字符串,為已經在使用的數組創建多個元素應該沒有問題。InternetAddress[] toAddresses


查看完整回答
反對 回復 2022-09-07
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

使用 InternetAddress.parse 方法。


查看完整回答
反對 回復 2022-09-07
  • 3 回答
  • 0 關注
  • 236 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號