Configuring Spring MailSender configuration for our SMTP server (but GMail works)

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:

# No username or password = no authentication
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.

!

+3
5

"mail.smtp.localhost" (thecompany.net).

+1

answer b, javamail smtp.mail.localhost

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

( ), SMTP- javaMailProperties?

<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>

, hostname JavaMail API.

+1
source

I think the problem with address resolution is in your case. make sure that the mail server is accessible using the specified name from the computer on which you deployed this code. if not just adding an entry to your hosts file.

0
source

SMTPTransport mailFrom () sets the sender address in the following order:

SMTPMessage.getEnvelopeFrom()
mail.smtp.from property
From: header in the message
System username using the InternetAddress.getLocalAddress() method

You just need to add the property fromin javaMailProperiesto your email address

0
source

Source: https://habr.com/ru/post/1708220/


All Articles