Pergunta

Eu tenho o código abaixo, mas não consigo descobrir como adicionar os destinatários CCO ao sendMessage.

Alguma ideia?

           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();
Foi útil?

Solução

Você precisa usar o seguinte método para adicionar vários destinatários

public void  addRecipients(Message.RecipientType type, Address[] addresses)

Também em vez da seguinte linha

transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));

Experimente isso

transport.sendMessage(message,message.getAllRecipients());

Outras dicas

        String [] to=new String[3];
        to[0]="adfsdf@gmail.com";
        to[1]="dffff@gmail.com";
        to[2]="ddddddddddsssssss@outlook.com";
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);

        }


        message.setSubject("Testing ");
        message.setText("dddd,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top