Search code examples
javasmtpjakarta-mailexchangewebservices

how can i send personalized email to each user


This is my code:

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);

This will send mail to all users but i want to send personalized mail to each users at once and user has to see their own mail Id in recipient To.


Solution

  • You will have to call the send method in the for loop.

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