开发者

Send mail to multiple receptions using JavaMail

    try
    {    
        String s1 = "example@gmail.com"; //sender (from)
        String s2 = request.getParameter("email");// (from jsp i am taking)


        String s3 = "testing mail"; (subject)
        String s4 = "hi.."; (message)

        Properties props=new Properties();

        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("sender ID","password");
                    }
                });


        Message message =new MimeMessage(session);

        message.setFrom(new InternetAddress(s1));
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(s2));
        message.setSubject(s3);
        message.setText(s4);

        Transport.send(message);
    }
    catch(Exception ex)
    {
        System.out.println("ERROR....."+ex);
    }

In this code, the email which I am taking as request parameter from JSP can be more than one, because the email address is coming from database.

How can I send the same email t开发者_开发问答o multiple recipients using JavaMail?


Nothing special, to send it to multiple users you need to pass array of InternetAddress in the property Message.RecipientType.TO.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜