Java Mail with To, CC and BCC

user2928305 picture user2928305 · Dec 5, 2013 · Viewed 34.8k times · Source

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

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

Answer

muthukumar picture muthukumar · Dec 5, 2013

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);