Email multiple recipients without revealing other recipients
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);
Answer by Camilo Daz Repka for Email multiple recipients without revealing other recipients
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.
Answer by dhable for Email multiple recipients without revealing other recipients
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.
Answer by aksarben for Email multiple recipients without revealing other recipients
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.
Answer by Anand for Email multiple recipients without revealing other recipients
Perfect program for sending mail to multiple users with attachments.
package home.test.source; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import javax.mail.internet.MimeMultipart; import java.util.Properties; class tester { @SuppressWarnings("static-access") public static void main(String args[]) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.stmp.user", "abc@gmail.com"); //If you want you use TLS props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.password", "password"); // If you want to use SSL props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String username = "abc@gmail.com"; String password = "password"; return new PasswordAuthentication(username,password); } }); String[] to = {"test1@gmail.com","test2@yahoo.in","test3@gmail.com","test4@gmail.com"}; String from = "abc@gmail.com"; String subject = "Testing..."; MimeMessage msg = new MimeMessage(session); try { msg.setFrom(new InternetAddress(from)); InternetAddress[] addressTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { addressTo[i] = new InternetAddress(to[i]); } msg.setRecipients(RecipientType.TO, addressTo); // msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); // msg.setText("JAVA is the BEST"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts msg.setContent(multipart ); Transport transport = session.getTransport("smtp"); transport.send(msg); System.out.println("E-mail sent !"); } catch(Exception exc) { System.out.println(exc); } } }
Answer by Maxy-B for Email multiple recipients without revealing other recipients
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.
Answer by Manjunath D R for Email multiple recipients without revealing other recipients
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. :)
Answer by titi for Email multiple recipients without revealing other recipients
as Message.RecipientType you should use Message.RecipientType.BCC to not showing the every address to every recipient
Google Keywords: Java Mail BCC
Answer by Naoufel ELYOUNOUSSI for Email multiple recipients without revealing other recipients
You can do this by setting the code as below
message.setRecipients(Message.RecipientType.BCC, toAddrs);
instead of
message.setRecipients(Message.RecipientType.TO, toAddrs);
Answer by james leeroy Dator for Email multiple recipients without revealing other recipients
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();
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment