Java Mail SMTP Hard bounce Issue

Viewed 267

We are using Java Mail 1.5 API to send mails. The SMTP host we are using for development, does not have any suppression list, hence, if any emails being invalid, like "xyz@xyz.com" will be sent and though its hard bounced back, we get it in the getInvalidAddresses() of exception,

but if we try a prod SMTP server, which has a suppression list, the same email "xyz@xyz.com" causes the other emails to NOT being sent, if they are part of the same TO list, like "valid@domain.com; xyz@xyz.com", our expectation is at-least "valid@domain.com" should get the email, but even that is not sent and getInvalidAddresses() is null

com.sun.mail.smtp.SMTPAddressFailedException: 254 4.7.1 - xyz@xyz.com is suppressed for sender smtp.xxx.xxx

public class SendMail {
    public static void main(String[] args) {
        final String username = "username@domain.com";
        final String password = "xxxxxxxxxxxx";
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "mysmtp.domain.com");
        prop.put("mail.smtp.port", "25");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.sendpartial", "true");
        prop.put("mail.smtp.socketFactory.port", "25");
        prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@domain.com"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse("valid@domain.com, xyz@xyz.com")
            );
            message.setSubject("Test Mail");
            message.setText("Test Mail");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

How can we handle suppression list errors in Java Mail API?

1 Answers

The problem you are experiencing is caused by the return code 254 used by the e-mail server. According to RFC 5321 (the most recent embodiment of RFC 821), there is no such code. This return code seems to be an invention of Oracle's Email Delivery.

Java Mail (and its successor Jakarta Mail) interprets (cf. source code) unknown:

  • 4xx return codes as validUnsentAddresses,
  • 5xx return codes as invalidAddresses,
  • 2xx return codes as complete delivery failure, since no educated guess can be done regarding the meaning of the code.

In this case the server should probably return a 550 code, meaning "command rejected for policy reasons".

Related