Problem configuring Spring's MailSender for our SMTP-server (but GMail works)

20,530

Solution 1

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

Solution 2

To add to complete the previous answer by matt b, the correct option of the javamail is smtp.mail.localhost

<property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.localhost">thecompany.net</prop>
    </props>
</property>

Solution 3

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.

Share:
20,530
Admin
Author by

Admin

Updated on August 13, 2020

Comments

  • Admin
    Admin over 3 years

    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!

  • Admin
    Admin almost 15 years
    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.
  • Admin
    Admin almost 15 years
    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.
  • Stephane
    Stephane over 9 years
    Is it smtp.mail... or mail.smtp... ?