开发者

Email multiple recipients without revealing other recipients

I'm using javamail to send emails to a list of recipients, but don't want them to be able to see who else received the email. I al开发者_运维知识库so don't want to send it using BCC since then the user doesn't even see themselves in the TO list. I thought this code would do it, but it shows all the recipients in the TO list. Other than creating a loop and sending the emails one at a time, is there another way to do this?

(NOTE: recipients[] is a string array containing the email addresses.)

javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[recipients.length];

for (int i = 0; i < recipients.length; i++)
{
    addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);
}

msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo); 


No, there isn't a way to do this with email.

You have to explicitly build and send an email iterating by each of your recipients, one of them as the sole member of your addressTo array.


The SMTP protocol doesn't care who's listed in the message and the recipients specified on the RCPT TO command are only used to figure out who to transport the message to. There's nothing stopping you from building the RFC822 message with the To header as you've defined above and then writing a custom SMTP client that send your particular message out but with a different set of recipients. And just because you can send the message doesn't mean a spam filter along the way is going to notice the wonky recipient headers and block the message.

In my experience, JavaMail's SMTP client is really good at sending basic messages without any of the mail tricks you often seen used by mailing list providers and spammers. Those companies spend a lot of effort to make sure they can send messages the way they want but they also are in a constant fight to make sure they're messages are treated as legit email.

Short answer: I'd resort to BCC and if this is for marketing purposes, consider using a company that specializes in this kind of thing.


Why are you concerned about the recipient not seeing his own address? He already knows his his own address, so why is it an issue? BCC was designed to handle exactly the problem you describe. It's been around for decades & sounds like a perfect fit.


Actually, we don't have to manually create InternetAddress objects for Multi Recepients. InternetAddress api provides a parse() method to do this for us. Sample code for this is as below,

msg.setRecipients(Message.RecipientType.TO,  InternetAddress.parse(toAddress));

Here parse method creates multiple InternetAddress objects if toAddress contains multiple email addresses seperated by ,(comma).

Check for below API for more details.

http://docs.oracle.com/javaee/6/api/javax/mail/internet/InternetAddress.html#parse(java.lang.String)

Happy Coding. :)


as Message.RecipientType you should use Message.RecipientType.BCC to not showing the every address to every recipient

Google Keywords: Java Mail BCC


Try this:

Session session = Session.getInstance(properties);
Transport transport = session.getTransport("smtp");
String recipient = "ex1@mail.com,ex2@mail.";
String[] recipients = recipient.split(",");
transport.connect(server, username, password);

for (String to : recipients) {

   Message message = new MimeMessage(session);
   message.setFrom(new InternetAddress(from));
   InternetAddress[] address = {new InternetAddress(to)};
   message.setRecipients(Message.RecipientType.TO, address);

   message.setSubject(subject);
   message.setText(body);
   message.setContent(body, "text/plain");
   message.saveChanges();
   transport.sendMessage(message, address);

}

transport.close();


According to the documentation for javax.mail.Transport:

public static void send(Message msg,
                        Address[] addresses)
                 throws MessagingException

Send the message to the specified addresses, ignoring any recipients specified
in the message itself.

So you should be able to put the actual delivery addresses (RCPT TO addresses) in the array argument to Transport.send, while putting whatever you want the recipients to see in the message headers via Message.setRecipient, MIMEMessage.addHeader, etc.

If you want different sets of users to see different things, you will have to construct and send a separate message for each set.


You can do this by setting the code as below

message.setRecipients(Message.RecipientType.BCC, toAddrs);

instead of

message.setRecipients(Message.RecipientType.TO, toAddrs);


Place your session creation inside the loop. It will create the session for every user but it's time complex.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜