【问题标题】:JavaMail does not detect invalid email address of receiver(address that does not exist)JavaMail 未检测到收件人的无效电子邮件地址(地址不存在)
【发布时间】:2012-11-29 16:38:12
【问题描述】:

我正在开发一个需要向客户发送提醒的网络应用。我正在使用 JavaMail。但是当我提供无效的接收者电子邮件地址(不存在的电子邮件 ID)时,程序 不会抛出 SendFailedException但发件人电子邮件 ID 的收件箱包含发送报告,说明邮件发送失败。有什么方法可以检测程序中的这种故障?使用的代码如下:

[注意使用的是gmail smpt]

 Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.stmp.user", "abc@gmail.com");          
        //If you want you use TLS 
        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "password");
        // If you want to use SSL
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                   "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String username = "abc@gmail.com";
                String password = "password";
                return new PasswordAuthentication(username,password); 
            }
        });
        String[] to = {"test1@gmail.com","test2@yahoo.in","test3@gmail.com","test4@gmail.com"};
        String from = "abc@gmail.com";
        String subject = "Testing...";
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] addressTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++)
            {
                addressTo[i] = new InternetAddress(to[i]);
            }
            msg.setRecipients(RecipientType.TO, addressTo); 
            // msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
            // msg.setText("JAVA is the BEST");

            // Create the message part 
            BodyPart messageBodyPart = new MimeBodyPart();

            // Fill the message
            messageBodyPart.setText("This is message body");

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            String filename = "file.txt";
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            msg.setContent(multipart );

            Transport transport = session.getTransport("smtp");
            transport.send(msg);
            System.out.println("E-mail sent !");
        }
        catch(Exception exc) {
            System.out.println(exc);
        }
    }
}

【问题讨论】:

    标签: jakarta-mail sendmail email-validation


    【解决方案1】:

    您可能误解了how "store and forward" works for email:发件人很少会立即知道地址无效。这很可能是无效域名的情况,但即使那样,您也不能保证您的 SMTP 会话会报告错误,因为这取决于您要交付的 MTA 如何处理电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-09
      • 2017-07-21
      • 2014-03-11
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多