使用 javax.mail 向 BCC 收件人发送电子邮件

The*_*ama 1 java jakarta-mail

我有下面的一段代码,但我不知道如何将 BCC 收件人添加到 sendMessage。

有任何想法吗?

           MimeMessage message = new MimeMessage(mailSession);

      String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar
              .getInstance().getTime());

      message.setSubject("This is my email for:" + today);
      message.setFrom(new InternetAddress("thesender@gmail.com"));
      String []to = new String []{"therecipient1@gmail.com"};
      String []bcc = new String[]{"therecipient2@gmail.com","therecipient3@gmail.com","therecipient4@gmail.com"};
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[0]));
      message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[0]));
      message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[1]));
      message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[2]));
      String body = theBody;
      message.setContent(body,"text/html");
      transport.connect();

        transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
        transport.close();
Run Code Online (Sandbox Code Playgroud)

Pra*_*lar 5

您需要使用以下方法添加多个收件人

public void  addRecipients(Message.RecipientType type, Address[] addresses)
Run Code Online (Sandbox Code Playgroud)

也代替以下行

transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
Run Code Online (Sandbox Code Playgroud)

尝试这个

transport.sendMessage(message,message.getAllRecipients());
Run Code Online (Sandbox Code Playgroud)