Email multiple recipients without revealing other recipients

前端 未结 8 1911
庸人自扰
庸人自扰 2020-12-24 08:20

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

相关标签:
8条回答
  • 2020-12-24 08:44

    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();
    
    0 讨论(0)
  • 2020-12-24 08:46

    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.

    0 讨论(0)
提交回复
热议问题