{"id":627,"date":"2014-01-08T00:47:38","date_gmt":"2014-01-07T22:47:38","guid":{"rendered":"http:\/\/mattiesworld.gotdns.org\/weblog\/?p=627"},"modified":"2014-01-08T00:47:38","modified_gmt":"2014-01-07T22:47:38","slug":"java-mail-and-smtp-501-helo-requires-valid-address","status":"publish","type":"post","link":"https:\/\/mattiesworld.gotdns.org\/weblog\/2014\/01\/08\/java-mail-and-smtp-501-helo-requires-valid-address\/","title":{"rendered":"Java mail and SMTP: &#8220;501 HELO requires valid address&#8221;"},"content":{"rendered":"<p>I was trying to send a mail through Java the other day and kept stumbling upon the following exception:<\/p>\n<blockquote><p>javax.mail.MessagingException: 501 HELO requires valid address<\/p><\/blockquote>\n<p>I thought it was caused by an authentication problem or my provider preventing me from sending from a random address.<br \/>\nThe former I tried to address by making sure I was fully authenticated as to prove I was not trying to send spam. In java mail the following properties are relevant for sending authenticated mail using TLS:<\/p>\n<blockquote><p>-Dmail.smtp.host=&lt;your smtp server&gt;<br \/>\n-Dmail.smtp.port=587<br \/>\n-Dmail.smtp.username=&lt;your username&gt;<br \/>\n-Dmail.smtp.password=&lt;your password&gt;<br \/>\n-Dmail.smtp.auth=true<br \/>\n-Dmail.smtp.starttls.enable=true<\/p><\/blockquote>\n<p>and setting an authenticator on the mail session object:<\/p>\n<blockquote><p>Session session = Session.getDefaultInstance(System.getProperties(), new javax.mail.Authenticator() {<br \/>\nprotected PasswordAuthentication getPasswordAuthentication() {<br \/>\nreturn new PasswordAuthentication(MAIL_USER, MAIL_PWD);<br \/>\n}<br \/>\n});<\/p><\/blockquote>\n<p>The latter I tried to address by sending from an alias that was associated with the original mailbox (user\/pwd combo). Yet I kept getting the same exception.<\/p>\n<p>Looking up the SMTP protocol and testing with telnet, I found out that normally you are supposed to provide a domain name when issueing the EHLO or HELO command. Since the api does not provide any methods for that, I looked for some more properties and indeed, it turns out you need to set<\/p>\n<blockquote><p>-Dmail.smtp.localhost<\/p><\/blockquote>\n<p>to make it provide the hostname for the EHLO command. According to the docs:<br \/>\n&#8220;<em>Should not normally need to be set if your JDK and your name service are configured properly.<\/em>&#8221; but apparently my case was not normal \ud83d\ude42<\/p>\n<p>Also worth noting is that you can turn on debug output with:<\/p>\n<blockquote><p>session.setDebug(true);<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to send a mail through Java the other day and kept stumbling upon the following exception: javax.mail.MessagingException: 501 HELO requires valid address I thought it was caused by an authentication problem or my provider preventing me from sending from a random address. The former I tried to address by making sure I &hellip; <a href=\"https:\/\/mattiesworld.gotdns.org\/weblog\/2014\/01\/08\/java-mail-and-smtp-501-helo-requires-valid-address\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java mail and SMTP: &#8220;501 HELO requires valid address&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-627","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/posts\/627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":3,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/posts\/627\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattiesworld.gotdns.org\/weblog\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}