Enviando el correo electrónico a los destinatarios de BCC utilizando javax.mail
-
21-12-2019 - |
Pregunta
Tengo la siguiente pieza de código, pero no puedo averiguar cómo agregar los destinatarios de BCC al Envío de Envío.
¿Alguna idea?
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();
Solución
Debe usar el siguiente método para agregar múltiples recientes
public void addRecipients(Message.RecipientType type, Address[] addresses)
también en lugar de la siguiente línea
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
Pruebe esto
transport.sendMessage(message,message.getAllRecipients());
Otros consejos
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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow