Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 also 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); 
like image 947
Don Avatar asked Dec 01 '10 00:12

Don


6 Answers

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.

like image 125
Camilo Díaz Repka Avatar answered Nov 19 '22 04:11

Camilo Díaz Repka


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.

like image 43
dhable Avatar answered Nov 19 '22 05:11

dhable


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.

like image 29
aksarben Avatar answered Nov 19 '22 05:11

aksarben


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. :)

like image 25
Manjunath D R Avatar answered Nov 19 '22 04:11

Manjunath D R


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

Google Keywords: Java Mail BCC

like image 1
titi Avatar answered Nov 19 '22 04:11

titi


Try this:

Session session = Session.getInstance(properties);
Transport transport = session.getTransport("smtp");
String recipient = "[email protected],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();
like image 1
jitd Avatar answered Nov 19 '22 04:11

jitd