I have some problems sending mails through SMTP using Spring’s MailSender interface and the concrete implementation JavaMailSenderImpl. I’m able to send mail through GMail, but not through our company SMTP-server (Postfix).
Correct configurations
To see that I have the correct configuration I used the excellent mail sender ssmtp. It’s a simple utility (which is able to emulate Sendmail) and is used solely for sending mail through SMTP.
Below are the two commands I used to send mail. The first one is for GMail and the second one for our company SMTP-server. Both mails arrived as they should and thus the configuration files that follow are correct.
$ ssmtp -C gmail-smtp.conf john.doe@gmail.com < gmail-message.txt
$ ssmtp -C other-smtp.conf john.doe@thecompany.net < other-message.txt
The contents of the ssmtp configuration files and the message files are listed below. How the configuration file is structured can be seen at: http://linux.die.net/man/5/ssmtp.conf:
gmail-message.txt:
To: john.doe@gmail.com
From: john.doe@gmail.com
Subject: Sent using the SMTP-server of GMail
Some content.
gmail-smtp.conf:
mailhub=smtp.gmail.com:587
UseSTARTTLS=yes
AuthUser=john.doe@gmail.com
AuthPass=john_password
other-message.txt:
To: john.doe@thecompany.net
From: john.doe@thecompany.net
Subject: Sent using the SMTP-server of TheCompany
Some content.
other-smtp.conf:
# No username or password = no authentication
hostname=thecompany.net
mailhub=mail.thecompany.net:25
MailSender configuration which works against GMail
I’m sucessful in sending mail through GMail using this Spring MailSender configuration:
...
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="john.doe@gmail.com" />
<property name="password" value="john_password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
...
The problem (sending through the company SMTP-server)
With this MailSender configuration:
...
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="mail.thecompany.net" />
<property name="port" value="25" />
</bean>
...
I get this exception:
org.springframework.mail.MailSendException; nested exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <rat>: Helo command rejected: need fully-qualified hostname
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:422)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:308)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)
... (The rest are methods I've created, which are irrelevant)
I also get 504 5.5.2 : Helo command rejected: need fully-qualified hostname if I remove hostname=thecompany.net from other-smtp.conf using ssmtp. I guess I have to provide the hostname somehow. My computers name is rat but it seems like it wants thecompany.net.
Any and all help appreciated!
Try to add the property “mail.smtp.localhost” with correct value (thecompany.net).