问题
I have a Java Program that was installed on an old Ubuntu machine and sent mail using javax.mail. However, that machine went down and I am now running the same Java app in a new CentOS machine.
However, I get an error "MessagingException: 501 Syntax: HELO hostname" when trying to send an email using mail.smtp.host = 127.0.0.1.
My guess is that the mailserver is not yet activated in this CentOS.
How would I go about configuring a mail server that javax.mail can use?
Thank you
回答1:
Your machine host name must be mapped in /etc/hosts
file.
Example:
Console shows: linux#
and cat /etc/hostname
shows
linux.mydomain.com
Then edit your hosts file running as root
. vi /etc/hosts
127.0.0.1 localhost linux linux.mydomain.com
Good detailed info can be found here: https://confluence.atlassian.com/display/CONFKB/Sending+Email+Fails+Due+to+501+Syntax%3A+HELO+Hostname+Error
回答2:
I encountered the same issue "MessagingException: 501 Syntax: HELO hostname" when sending out email with Spring MailSender. What works for me is to add in extra property "mail.smtp.localhost" under javaMailProperties like below:
<!-- JAVA MAIL -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="" />
<property name="port" value="25" />
<property name="protocol" value="smtp" />
<property name="username" value="" />
<property name="password" value="" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
<prop key="mail.smtp.localhost">localhost</prop>
</props>
</property>
</bean>
回答3:
The problem is that the naming service on the new machine is not properly configured and Java can't find the host name of the machine. The SMTP HELO command needs to include the host name. The server is complaining because it's missing. Turn on JavaMail Session debugging and you can see the actual command this is sent. You can work around this host configuration problem by setting the JavaMail property mail.smtp.localhost to be the host name you want to use in the HELO command.
回答4:
You need to run sendmail
. See here for more info. Configuring sendmail
can be a chore and you may want to take the configuration sendmail.cf
from the old machine if possible.
I suspect (also) that you should have some central MTA (mail transport agent) set up, such that all machines in your enterprise use this, rather than running one per host. i.e. not using localhost
.
回答5:
In my case etc/hostname was susetest(and not linux.company.com)
Modified etc/hosts from 127.0.0.1 localhost to 127.0.0.1 localhost susetest(also after localhost make sure to use tab, when modifying the file)
Make sure to save changes, postfix stop, postfix start to restart SMTP server.
Should work fine.
(adding properties.setProperty("mail.smtp.localhost", "ourcompany.com"); to the properties also solved the problem, but workaround should not be the fix, when you can find the root cause, even if it takes days in my case)
来源:https://stackoverflow.com/questions/13840444/mail-server-with-javax-mail-and-centos