【问题标题】:Send mail to an array list of recipients将邮件发送到收件人数组列表
【发布时间】:2023-03-16 23:34:01
【问题描述】:

我在这里得到了这个教程......

how to send an email from jsp/servlet?

但是,如果我有来自我想要发送电子邮件的 DATABASE 的电子邮件地址列表,该怎么办

TestMail 类

public class TestMail {
    public static void main(String... args) throws Exception {
        // Create mailer.
        String hostname = "smtp.example.com";
        int port = 2525;
        String username = "nobody";
        String password = "idonttellyou";
        Mailer mailer = new Mailer(hostname, port, username, password);

        // Send mail.
        String from = "john.doe@example.com";
        String to = "jane.doe@example.com";
        String subject = "Interesting news";
        String message = "I've got JavaMail to work!";
        mailer.send(from, to, subject, message);
    }
}

jsp

<form action="contact" method="post">
    <p>Your email address: <input name="email"></p>
    <p>Mail subject: <input name="subject"></p>
    <p>Mail message: <textarea name="message"></textarea></p>
    <p><input type="submit"><span class="message">${message}</span></p>
</form>

小服务程序

public class ContactServlet extends HttpServlet {
    private Mailer mailer;
    private String to;

    public void init() {
        // Create mailer. You could eventually obtain the settings as
        // web.xml init parameters or from some properties file.
        String hostname = "smtp.example.com";
        int port = 2525;
        String username = "nobody";
        String password = "forgetit";
        this.mailer = new Mailer(hostname, port, username, password);
        this.to = "you@example.com";
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String subject = request.getParameter("subject");
        String message = request.getParameter("message");
        // Do some validations and then send mail:

        try {
            mailer.send(email, to, subject, message);
            request.setAttribute("message", "Mail succesfully sent!");
            request.getRequestDispatcher("/WEB-INF/contact.jsp").forward(request, response);
        } catch (MailException e) {
            throw new ServletException("Mailer failed", e);
        }
    }
}

【问题讨论】:

    标签: java jsp servlets jakarta-mail netbeans-7


    【解决方案1】:
          ArrayList email= new ArrayList();
          while(rs.next()) {
    
                    email.add(rs.getString("column_name"));
          }
    
    
        Message message = new MimeMessage(session);
    
       InternetAddress[] address = new InternetAddress[email.size()];
        for (int i = 0; i < email.size(); i++) {
            address[i] = new InternetAddress(email.get(i));
        }
         message.setRecipients(Message.RecipientType.TO, address);
    

    【讨论】:

    • 对不起,我是新手 ---------------------------------- ---String[] 收件人={"my@djksd.com","dsjkfh@ghsg.com"};我会用什么来替换 my@djksd.com","dsjkfh@ghsg.com 以获得结果集
    • 这有什么新东西??? :o 你知道ArrayList,但不知道Array。这对我来说是一个很大的惊喜..
    • 如果它来自结果集,我会怎么做?
    • 你的意思是来自数据库?
    • 是的......我会写什么来代替这个 -- String[] recipient={"my@djksd.com","dsjkfh@ghsg.com"};
    【解决方案2】:

    您可以在 init 方法中检索电子邮件的 ArrayList,将 ArrayListString 转换为 Address 对象的数组。

    在您的消息的setRecipients() 方法中传递该数组,您就完成了。

    例子:

    ArrayList<String> listOfEmails    = SOME DB CALL TO GET ARRAYLIST OF EMAILS;
    ArrayList<InternetAddress> listOfToAddress = new ArrayList<InternetAddress>();
    
    for (String temp : listOfEmails) {
        if (temp != null) {
            listOfToAddress.add(new InternetAddress(temp));
        }
    }
    

    【讨论】:

      【解决方案3】:
      ArrayList email= new ArrayList();
        while(rs.next()) {
      
                  email.add(rs.getString("column_name"));
        }
      
      
      Message message = new MimeMessage(session);
      
      InternetAddress[] address = new InternetAddress[email.size()];
      for (int i = 0; i < email.size(); i++) {
          address[i] = new InternetAddress(email.get(i));
      }
       //message.setRecipients(Message.RecipientType.TO, address); this not work
        message.addRecipients(Message.RecipientType.TO, address);
      

      它有效..试试这个。

      【讨论】:

        【解决方案4】:
        ArrayList<String> listOfEmails = //getListofEmails
        
        InternetAddress[] address = listOfEmails.stream()
                         .toArray(InternetAddress[]::new);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-05
          • 2014-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-28
          相关资源
          最近更新 更多