发送电子邮件的Java代码仅适用于一个电子邮件ID?

时间:2014-04-24 02:27:32

标签: java email

我使用java中的以下代码向我的团队中的人发送电子邮件但是它仅在public static String to = "marsh@gmail.com";时有效但如果我public static String to = "marsh@gmail.com, mark@gmail.com";不确定我缺少什么就行不通有人帮我同时向多个身份证发送电子邮件?目前代码一次只能发送给一个人?

public static String to = "marsh@gmail.com, mark@gmail.com";
public static String from= "SANDBOX";
public static String host = "localhost"; 

public  static void send_production_email(String reportDate){

    System.out.println("Preparing to Send Email to Admin's...");

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);

      Session session = Session.getDefaultInstance(properties);

      try{

         MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress(from));
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

       message.setSubject(" Production Database Backed up Successfully");

       message.setContent("<h4> Production Database Backup Completed on" +reportDate+" </h4><br>"   
            + "<h4>Please do not respond to this email as this is an auto generated email</h4></br>"
            +"<h4>Thank You!</h4></br>" );
       Transport.send(message);
         System.out.println("Sent message successfully....");
      }
      catch (MessagingException mex) {
         mex.printStackTrace();
      }

}

2 个答案:

答案 0 :(得分:2)

您需要为每个电子邮件地址致电message.addRecipient()或使用地址数组致电addRecipients()

答案 1 :(得分:0)

这不是指定多个收件人的方式;简而言之,你必须使用一个&#34;字符串&#34;每个地址&#34;,只是为了让您理解,尝试暂时修改您的代码:

// cahnge temporary:
public static String to = "marsh@gmail.com";
public static String to2 = "mark@gmail.com";

还有:

message.setRecipients(Message.RecipientType.TO,
        new InternetAddress[] {new InternetAddress(to), new InternetAddress(to2)});