带有收件人、抄送和密件抄送的 Java 邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20402728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Mail with To, CC and BCC
提问by user2928305
I am trying to send mail with to, cc and bcc. I am using javax.mail for achieving this. Please find below a part of my code
我正在尝试使用 to、cc 和 bcc 发送邮件。我正在使用 javax.mail 来实现这一点。请在下面找到我的代码的一部分
InternetAddress[] myToList = InternetAddress.parse("[email protected],[email protected]");
InternetAddress[] myBccList = InternetAddress.parse("[email protected]");
InternetAddress[] myCcList = InternetAddress.parse("[email protected]");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(objEmail.getFrom()));
message.setRecipients(Message.RecipientType.TO,myToList);
message.setRecipients(Message.RecipientType.BCC,myBccList);
message.setRecipients(Message.RecipientType.CC,myCcList);
But when I try to execute this code, I am getting the below exception:
但是当我尝试执行此代码时,出现以下异常:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 452 4.5.3 Too many recipients
javax.mail.SendFailedException:无效地址;
嵌套异常是:
com.sun.mail.smtp.SMTPAddressFailedException: 452 4.5.3 收件人太多
回答by muthukumar
Try this
尝试这个
InternetAddress[] myToList = InternetAddress.parse("[email protected],[email protected]");
InternetAddress[] myBccList = InternetAddress.parse("[email protected]");
InternetAddress[] myCcList = InternetAddress.parse("[email protected]");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(objEmail.getFrom()));
message.setRecipients(Message.RecipientType.TO,myToList);
// changes,...
message.addRecipient(Message.RecipientType.BCC,myBccList);
message.addRecipient(Message.RecipientType.CC,myCcList);
回答by said_dev
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of toaddresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
InternetAddress[] ccAddress = new InternetAddress[cc.length];
// To get the array of ccaddresses
for( int i = 0; i < cc.length; i++ ) {
ccAddress[i] = new InternetAddress(cc[i]);
message.addRecipient(Message.RecipientType.CC, ccAddress[i]);
}
InternetAddress[] bccAddress = new InternetAddress[bcc.length];
// To get the array of bccaddresses
for( int i = 0; i < bcc.length; i++ ) {
bccAddress[i] = new InternetAddress(bcc[i]);
message.addRecipient(Message.RecipientType.BCC, bccAddress[i]);
}
The not us .setRecipients if you need use CC or BCC
如果您需要使用 CC 或 BCC,则不是我们 .setRecipients