How to check status of an SMTP server from Java?

走远了吗. 提交于 2019-12-12 05:19:30

问题


In a Java program, what is the best way of determining if an SMTP server is ready to accept and send an email?


回答1:


You can use JavaMail API and try to send an email.

If you don't want to actually send the email, you can open a TCP socket to port number 25 of your mail server and send the following commands:

HELO yourdomain.com
MAIL FROM: youremail@yourdomain.com
RCPT TO: recipient@recipientdomain.com

Make sure you check all server responses after each command issued. If you don't see any errors until that point, then probably you will be able to send emails with that SMTP server.

And, as Jordan Stewart kindly appointed:

You'll also want to make sure that the HELO domain has a complementary A / PTR dns record pair, with the IP address the domain is mapped to being the IP address you're connecting from. The domain also needs at least one MX record. I.e. if the server you're connecting from is 123.45.67.89 then you'd need an A record mapping yourdomain.com to 123.45.67.89 and a PTR ("reverse DNS") record mapping 123.45.67.89 back to yourdomain.com. If any of these conditions aren't met you'll run into problems with some mail servers as checks for these things are anti-spam measures.



来源:https://stackoverflow.com/questions/1704749/how-to-check-status-of-an-smtp-server-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!