2014-04-24 128 views
0

下面的代码在java中用于发送电子邮件给我的团队中的人但是,它只适用于如果public static String to = "[email protected]";但它不会工作,如果我有public static String to = "[email protected], [email protected]";不知道我错过了什么,可以有人帮我发送电子邮件到多个ID与此同时 ?目前代码一次只能发送给一个人?Java代码发送电子邮件只适用于一个电子邮件ID?

public static String to = "[email protected], [email protected]"; 
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(); 
     } 

} 
+0

你试过添加多个收件人? – chrylis

+0

Just have:String [] to = {“[email protected]”,“[email protected]”};并调用message.addRecipient(Message.RecipientType.TO,new InternetAddress(to [0])); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to [1])); –

+0

你真的想要所有的消息都是h4标签吗? tripleee

回答

2

你需要调用message.addRecipient()每个电子邮件地址或addRecipients()与地址数组。

0

这不是指定多个收件人的方式;总之,你必须使用一个“串”每“地址”,只是让你了解,尝试修改暂时将您的代码是这样的:

// cahnge temporary: 
public static String to = "[email protected]"; 
public static String to2 = "[email protected]"; 

也:

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