javax.mailを使用してBCC受信者に電子メールを送信する
-
21-12-2019 - |
質問
下記のコードを持っていますが、SendMessageにBCC受信者を追加する方法を理解することはできません。
任意のアイデア?
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();
. 解決
複数のRepiepientを追加するには、次の方法を使用する必要があります。
public void addRecipients(Message.RecipientType type, Address[] addresses)
.
次の行の代わりに
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
.
これを試してください
transport.sendMessage(message,message.getAllRecipients());
. 他のヒント
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);
. 所属していません StackOverflow