简体   繁体   中英

Sending mail using JavaMail - port 25, STARTTLS, authentication

I'm trying to send an email message to a SMTP server that listens on port 25, uses STARTTLS and requires authentication.

As far as I understand, the client should

  • greet the server with EHLO clientName
  • initiate TLS using STARTTLS
  • authenticate itself using AUTH LOGIN
  • go on with deliverying the email using RCPT TO , etc

My simplified code is

String protocol = "smtp";

Properties props = new Properties();
props.put("mail.debug", "true");
props.put("mail." + protocol + ".auth", true);
props.put("mail." + protocol + ".host", smtpHost);
props.put("mail." + protocol + ".starttls.required", true);

Session session = Session.getInstance(props);

InternetAddress[] recipients = InternetAddress.parse(username);

Message message = buildMessage(session, username, recipients);

Transport t = session.getTransport(protocol);
t.connect(username, password);

t.sendMessage(message, recipients);

As far as I understand this should work, but the debug output shows that it hangs at

STARTTLS
220 2.0.0 Ready to start TLS

Removing the starttls.required property leads to the server denying access, since the AUTH command is never present unless STARTTLS is issued

MAIL FROM:<deliverytest@somedomain.com>
250 2.1.0 Ok
RCPT TO:<deliverytest@somedomain.com>
554 5.7.1 Service unavailable; Client host [50.16.63.26] blocked using zen.spamhaus.org; http://www.spamhaus.org/query/bl?ip=50.16.63.26

What's the proper incantation to get Javamail to work with my setup?

Two things are wrong here

  1. It appears the remote does not support TLS on port 25
  2. It connected successfully WITHOUT TLS, but the IP address you're sending from is identified as a SPAM sender (or maybe just an ISP DHCP address) on Spamhaus, and the use of that server to send mail is blocked.

If you follow the link given in the error ( http://www.spamhaus.org/query/bl?ip=50.16.63.26 ) you get to a page that says

Ref: PBL1522093

50.16.0.0/16 is listed on the Policy Block List (PBL)

Outbound Email Policy of Amazon Web Services EC2 for this IP range:

It is the policy of Amazon Web Services EC2 that unauthenticated email sent from this IP address should be sent out only via the designated outbound mail server allocated to Amazon Web Services EC2 customers. To find the hostname of the correct mail server to use, customers should consult the original signup documentation or contact Amazon Web Services EC2 Technical Support.

In other words, you must send all outgoing mail via the SMTP server provided for you by Amazon. You are not permitted to sent email directly, and any receiving host that uses Spamhaus (many, if not most) will block your attempts to do so.

You're doing it right. Are you sure it's hanging and not throwing an unexpected exception? If it's hanging, what's the stack trace show?

Turns out that the original code was working. I was sent of by two mistakes:

  • I did not check the actual email account to see whether the email was delivered - and it was.
  • I was using a thread pool which was not shut down therefore the program appeared to be hanging

OTOH, it would be nice for the Javamail debug mode to let me know when the message was actually sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM