如何向每个用户发送个性化电子邮件

时间:2017-12-27 09:09:36

标签: java smtp javamail exchangewebservices

这是我的代码:

if((recipientsBcc != null) && (recipientsBcc.length > 0)){          
addressBcc = new InternetAddress[recipientsBcc.length];
}

MimeMessage message = new MimeMessage(session);  
message.setFrom(new InternetAddress(Receipt_From));
if(addressBcc != null){
for (int i = 0; i < recipientsBcc.length; i++) {
addressBcc[i] = new InternetAddress(recipientsBcc[i]);
}
message.setRecipients(Message.RecipientType.BCC, addressBcc); 
Transport.send(message);

这会向所有用户发送邮件,但我想立即向每个用户发送个性化邮件,用户必须在收件人To中查看自己的邮件ID。

1 个答案:

答案 0 :(得分:1)

你必须在for循环中调用send方法。

if((recipientsBcc != null) && (recipientsBcc.length > 0))
{          
    addressBcc = new InternetAddress[recipientsBcc.length];
}
MimeMessage message = new MimeMessage(session);  
message.setFrom(new InternetAddress(Receipt_From));
if(addressBcc != null)
{
    for (int i = 0; i < recipientsBcc.length; i++) 
    {
        addressBcc[i] = new InternetAddress(recipientsBcc[i]);
        //This will send indiviaual emails.
        message.setRecipients(Message.RecipientType.TO, addressBcc); 
        Transport.send(message);
    }
}