I have problems sending emails via SMTP using the Spring MailSender interface and a specific implementation of JavaMailSenderImpl . I can send mail through GMail, but not through our SMTP server of our company ( Postfix ).
Correct configurations
To see that I have the correct configuration, I used the excellent ssmtp mail sender . This is a simple utility (capable of emulating Sendmail) and is used exclusively for sending mail through SMTP.
Below are the two commands that I used to send mail. The first is for GMail, and the second is for our SMTP server. Both letters arrived as they should, and therefore the corresponding configuration files 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 message files are shown below. How a structured configuration file 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:
hostname=thecompany.net
mailhub=mail.thecompany.net:25
MailSender configuration that works against GMail
I was able to send 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>
...
Problem (sending via company SMTP server)
Using 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 504 5.5.2: Helo : , hostname = thecompany.net other-smtp.conf, ssmtp, , - . - , , thecompany.net.
!