views:

3624

answers:

3

Hi, 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 [email protected] < gmail-message.txt 
$ ssmtp -C other-smtp.conf [email protected] < 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: [email protected]
From: [email protected]
Subject: Sent using the SMTP-server of GMail

Some content.

gmail-smtp.conf:

mailhub=smtp.gmail.com:587
UseSTARTTLS=yes
[email protected]
AuthPass=john_password

other-message.txt:

To: [email protected]
From: [email protected]
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="[email protected]" />
    <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!

A: 

I think its an address resolution problem in your case. make sure the mail server is accessible using the given name from the machine you've deployed this code on. if not just add an entry to your the hosts file.

MahdeTo
I can send from the same machine using ssmtp. And if I can't connect I get another exception. So it's not an address resoultion problem.
DeletedAccount
A: 

Can you pass in the extra properties (hostname) you need to set with your SMTP server in the javaMailProperties property?

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="host" value="mail.thecompany.net" />
    <property name="port" value="25" />
    <!-- addition: -->
    <property name="javaMailProperties">
        <props>
            <prop key="hostname">thecompany.net</prop>
        </props>
    </property>
</bean>

Not quite sure what the correct key name is for hostname in the JavaMail API.

matt b
Maybe. I have looked at the key names, thought I'm not sure I've seen them all, but I've found no hostname key. All keys seem to start with mail.something.
DeletedAccount
+1  A: 

Try to add the property "mail.smtp.localhost" with correct value (thecompany.net).

Karimchik