4

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("gopi.mani@xyz.com,Maimsa.SF@xyz.com");
InternetAddress[] myBccList = InternetAddress.parse("Usha.B@xyz.com");
InternetAddress[] myCcList = InternetAddress.parse("NEHA.SIVA@xyz.com");
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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user2928305
  • 85
  • 1
  • 1
  • 9
  • Have you set the properties ? – muthukumar Dec 05 '13 at 14:37
  • "452 4.5.3 Too many recipients" leads me to believe a SMTP server is rejecting your email instead of a Java issue. Have you tried with just a single recipient to see if you continue to get this error? – Steven V Dec 05 '13 at 14:39
  • Yeah i have set the properties like below, Properties props = new Properties(); props.put("mail.smtp.auth", "false"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host_name); props.put("mail.smtp.port", port); – user2928305 Dec 05 '13 at 14:46
  • Hi Steven, If i use "message.setRecipients(Message.RecipientType.TO,myToList);" alone and remove "BCC" and "CC" lines of code. My code is working fine. Please confirm if this is still a Java issue or the issue is with the SMTP server – user2928305 Dec 05 '13 at 14:48
  • 1
    @user2928305: That error message comes directly from the SMTP server. – SLaks Dec 05 '13 at 15:02
  • @SLaks : Then my SMTP setting needs to be changed for me to send mails with CC and BCC right. – user2928305 Dec 05 '13 at 15:11
  • thanks steven and SLaks. – user2928305 Dec 05 '13 at 15:47

2 Answers2

9

Try this

InternetAddress[] myToList = InternetAddress.parse("gopi.mani@xyz.com,Maimsa.SF@xyz.com");
InternetAddress[] myBccList = InternetAddress.parse("Usha.B@xyz.com");
InternetAddress[] myCcList = InternetAddress.parse("NEHA.SIVA@xyz.com");
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);
nlucaroni
  • 47,556
  • 6
  • 64
  • 86
muthukumar
  • 2,233
  • 3
  • 23
  • 30
1
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

said_dev
  • 31
  • 2