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 sinc
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.