编程语言
首页 > 编程语言> > 使用JavaMail将邮件发送到多个接收方

使用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);
    }

在此代码中,由于电子邮件地址来自数据库,因此我从JSP用作请求参数的电子邮件可以不止一个.

如何使用JavaMail将同一封电子邮件发送给多个收件人?

解决方法:

没什么特别的,要将其发送给多个用户,您需要在属性Message.RecipientType.TO中传递InternetAddress数组.

标签:javamail,java
来源: https://codeday.me/bug/20191208/2088883.html