开发者

Send email to list of users with different timezones?

开发者 https://www.devze.com 2023-01-03 09:51 出处:网络
I use the following method to send email to list of users. I wa开发者_运维问答nt the (To) in each email to be for just the user only, not all users.

I use the following method to send email to list of users.

  • I wa开发者_运维问答nt 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)
    {
    }


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消