当其中一个地址无效时,为什么Transport.sendMessage失败?

时间:2012-08-23 07:21:01

标签: java email

我使用以下代码发送邮件

public void send() throws MessagingException
    {
        // create some properties and get the Session
        Properties props = new Properties();
        props.put("mail.smtp.host", this.getSMTPServer());

        if( this.getDebugMode() )
            props.put("mail.debug", "true");
        else
            props.put("mail.debug", "false");
        //Comment by Sandip for FIRSTMEDIA-578
        //props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props, null);

        MimeMessage mail = new MimeMessage(session);

        //Set Mailer string in Header.. 
        mail.setHeader("X-Mailer", this.getMailer() );

        //Set TO Recipients, toList would be of comma separated if multiple addresses are there
        if( this.getTo() != null )
        {
            StringTokenizer st = new StringTokenizer(this.getTo(),",");
            InternetAddress[] recList = new InternetAddress[st.countTokens()];

            for (int r = 0; st.hasMoreTokens(); r++)
                recList[r] = new InternetAddress(st.nextToken().trim());          

            if(recList.length != 0 )
            {
                mail.setRecipients(Message.RecipientType.TO, recList);
            }

        }           


        //Set CC Recipients, bccList would be of comma separated if multiple addresses are there
        if( this.getCc() != null )
        {
            StringTokenizer st1 = new StringTokenizer(this.getCc(),",");
            InternetAddress[] copyList = new InternetAddress[st1.countTokens()];
            for (int c = 0; st1.hasMoreTokens(); c++)
                copyList[c] = new InternetAddress(st1.nextToken().trim());          

            if(copyList.length != 0 )
                mail.setRecipients(Message.RecipientType.CC, copyList);
        }           


        //Set BCC Recipients, bccList would be of comma separated if multiple addresses are there
        if( this.getBcc() != null )
        {
            StringTokenizer st2 = new StringTokenizer(this.getBcc(),",");
            InternetAddress[] bcopyList = new InternetAddress[st2.countTokens()];
            for (int c = 0; st2.hasMoreTokens(); c++)
                bcopyList[c] = new InternetAddress(st2.nextToken().trim());          

            if(bcopyList.length != 0 )
                mail.setRecipients(Message.RecipientType.BCC, bcopyList);
        }   


        // Create a mime message
        mail.setFrom(new InternetAddress(this.getFrom()));
        mail.setSubject(subject);


        //create mulitple parts to added
        Multipart mp = new MimeMultipart();

        MimeBodyPart mbp1 = new MimeBodyPart();

        //messageMIME can be "text/plain" or "text/html" or anything related to mime
        mbp1.setContent(this.getMsgText(),this.getContentType());
        mp.addBodyPart(mbp1);

        //Adding attachments to mail
        if( this.attachmentList != null )
        {
            for(int i=0; i < this.attachmentList.size() ; i++ )
            {
                MimeBodyPart mbp = new MimeBodyPart();
                FileDataSource fds = new FileDataSource((File)this.attachmentList.elementAt(i));
                mbp.setDataHandler(new DataHandler(fds));
                mbp.setFileName(fds.getName());
                mp.addBodyPart(mbp);
            }
        }

        mail.setContent(mp);
        mail.saveChanges();

        mail.setSentDate(new Date());

        // Send the message
        Transport trans = session.getTransport("smtp");
        trans.connect(this.getSMTPServer(), this.getSMTPUsername(), this.getSMTPPassword());
        trans.sendMessage(mail, mail.getAllRecipients());
        trans.close();

    }

使用此功能当收件人列表中的一个收件人发送邮件失败时,所有收件人邮件发送都将失败。

是否有任何更改要求从列表中向所有正确的邮件收件人发送邮件?

1 个答案:

答案 0 :(得分:0)

如果您查看Doc,请发送消息说: “将消息发送到指定的地址列表。指示传递状态的适当TransportEvent将传递到此传输上注册的任何TransportListener。此外,如果任何地址无效,则抛出SendFailedException。消息是否仍然是成功发送到任何有效地址取决于传输实现。“

您可以尝试:

  • 在您的代码或
  • 中将其发送一个
  • 您可以捕获异常SendFailedException并进行检查 getValidUnsentAddresses发送到有效的地址 尚未发送。

我真的更喜欢第二个:)

检查Sent Failed Exception