2

I got this tutorial here...

how to send an email from jsp/servlet?

However What if I have list of email addresses from DATABASE where I want to send the email to

TestMail class

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>

Servlet

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);
        }
    }
}
Community
  • 1
  • 1
toink
  • 255
  • 4
  • 11
  • 30

4 Answers4

6
      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);
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • sorry I'm new to this here -------------------------------------String[] recipient={"my@djksd.com","dsjkfh@ghsg.com"}; what would I put to replace my@djksd.com","dsjkfh@ghsg.com to get the resultset – toink Nov 08 '13 at 08:00
  • 2
    what is new in this ??? :o You know `ArrayList`, but don't know `Array`. It's big surprise for me.. – Ravi Nov 08 '13 at 08:00
  • if it is coming from the resultset what would I do? – toink Nov 08 '13 at 08:02
  • you mean from database ? – Ravi Nov 08 '13 at 08:02
  • yes... what would I write in place of this -- String[] recipient={"my@djksd.com","dsjkfh@ghsg.com"}; – toink Nov 08 '13 at 08:03
1

You can retrieve ArrayList of emails in your init method, convert that ArrayList of String to array of Address objects.

Pass that array in setRecipients() method of your message and you're done.

Example :

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));
    }
}
Anuj Patel
  • 17,261
  • 3
  • 30
  • 57
0
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);

it works.. try this.

BURHANUDDIN
  • 19
  • 1
  • 6
0
ArrayList<String> listOfEmails = //getListofEmails

InternetAddress[] address = listOfEmails.stream()
                 .toArray(InternetAddress[]::new);
Unheilig
  • 16,196
  • 193
  • 68
  • 98