向具有不同内容的多个收件人发送电子邮件

时间:2014-11-07 12:46:21

标签: java javamail sendmail

我正在尝试使用JavaMail向多个收件人发送邮件。每条消息的内容都是不同的。该程序向不同的收件人发送电子邮件,但内容相同。谁能帮我。她是我的源代码。

public static void postMail(String[] recipients, String subject, Timestamp time)throws MessagingException {
 String nachricht = null;
 String link = null;
 Session session = MailUtil.getGMailSession("username", "password");
 Message msg = new MimeMessage(session);

 msg.setSubject(subject);


for(int i=0; i<=recipients.length-1;i++){
 System.out.println(recipients[i]);
 link = recipients[i] + "/" + time; 
 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients[i]));
}//endOf forSchleife
message = link
msg.setContent(message, "text/plain");
Transport.send(msg);
}

1 个答案:

答案 0 :(得分:0)

I think that the code should be like below:

    for(int i=0; i<=recipients.length-1;i++){
         Message msg = new MimeMessage(session);
         msg.setSubject(subject);
         System.out.println(recipients[i]);
         link = recipients[i] + "/" + time; 
         //msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients[i]));
         //Try this please            
         msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[i]));

message = link
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }//endOf forSchleife
inside of the for loop...