Invio di e-mail ai destinatari BCC utilizzando Javax.mail
-
21-12-2019 - |
Domanda
Ho il seguente pezzo di codice, ma non riesco a capire come aggiungere i destinatari BCC al sendomessaggio.
Qualche 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();
. Soluzione
È necessario utilizzare il seguente metodo per aggiungere più recipienti
public void addRecipients(Message.RecipientType type, Address[] addresses)
.
anche invece della seguente riga
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
.
Prova questo
transport.sendMessage(message,message.getAllRecipients());
. Altri suggerimenti
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);
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow