带有To,CC和BCC的Java Mail

时间:2013-12-05 14:34:37

标签: java email bcc carbon-copy

我正在尝试使用to,cc和bcc发送邮件。我正在使用javax.mail来实现这一目标。请在下面找到我的代码的一部分

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

但是当我尝试执行此代码时,我得到以下异常:

  

javax.mail.SendFailedException:无效的地址;
  嵌套异常是:
  com.sun.mail.smtp.SMTPAddressFailedException:452 4.5.3收件人太多

2 个答案:

答案 0 :(得分:6)

试试这个

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

答案 1 :(得分: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]);
        }

如果需要使用CC或BCC,则不是我们的.setRecipients