I use the following method to send email to list of users.
I want the (To) in each email to be for just the user only, not all users. That is, appears to the users that the email is sent to only him/her. My guess is to loop on
message.addRecipients(Message.RecipientType.TO, address);
then send the message, right? But this is a heavy process sending an email many times. What can I do?
- Suppose that I have the timezone for each user and I want to send each user the message in his/her timzone. The same issue, I guess, setting sent date for each user in his/her timezone then sending the message, right?
The method is:
try
{
Properties props = System.getProperties();
props.put("mail.smtp.host", "localhost");
// Get a mail session
Session session = Session.getDefaultInstance(props, null);
// Define a new mail message
Message message = new MimeMessage(session);
InternetAddress ia = new InternetAddress();
ia.setPersonal("MySite");
ia.setAddress(from);
message.setFrom(ia);
Address[] address = new Address[recievers.size()];
for (int i = 0; i < recievers.size(); i++) {
address[i] = new InternetAddress(recievers.get(i));
}
message.addRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html");
// use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
// add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
// Put all message parts in the message
message.setContent(multipart);
message.setSentDate(getCurrentDate());
// Send the message
Transport.send(message);
}
catch (Exception ex)
{
}