tags:

views:

38

answers:

1

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)
    {
    }
+2  A: 

The simplest way to send an email to multiple recipients without each of them seeing the list is to use BCC (blind carbon copy):

message.addRecipients(Message.RecipientType.BCC, address);

When each recipient gets their message, they only see their own address. You still need to set a TO, but you can use a single fixed dummy address for that.

I'm not sure what you're asking about the sent date, though. The sent time on an email is the time (and timezone) it was sent by the sending agent, and it's up to the recipients's email software to render that in the correct local timezone.

Or do you mean you want to fake the sent time for each recipient?

skaffman
There's no requirement in the SMTP spec to have the To: field set in the message header.
dty
@Danny: Is that right? I didn't know that...
skaffman
i want set a To for each user and send the email in the client time not the server mail coz this lead to incorrect receiving timing.
@ylazez: What does "incorrect receiving timing" mean? Please give a concrete example.
skaffman
You can always put your own email address in the To: field. Some email applications, like Lotus Notes, require an email address in the To: field.
Gilbert Le Blanc