Search code examples
javaemailtomcattomcat8

Cannot send email with Java in Test Server


I'm using Java Email Sender to send emails in Java.. And I'm using VelocityEngine to send HTML emails.

In my local computer everything is good! The emails are sent.

But when I deploy the code to the test server (which don't have a domain related, just the IP) The connection with gmail fails.

I have my email setting in spring-config.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="mail.domain.com" />
        <property name="port" value="25" />
        <property name="username" value="mymail@domain.com" />
        <property name="password" value="*xxxxxx" />

        <property name="javaMailProperties">
           <props>
                  <prop key="mail.smtp.auth">true</prop>
                  <prop key="mail.smtp.starttls.enable">false</prop>
                  <prop key="mail.smtps.ssl.checkserveridentity">true</prop>
                  <prop key="mail.smtps.ssl.trust">*</prop>
               </props>
        </property>
    </bean>

When I try to send the email from the server the error I get said:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: mail.domain.com, port: 25;

And then it said, that it is a timeout.. But my question is if is there any security that I need to disabled from the email server or if it's something with tomcat


Solution

  • Finally I was able to fix this issue using TLS security in the spring-config

    So I just have to add

    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
    <prop key="mail.smtps.ssl.checkserveridentity">true</prop>
    <prop key="mail.smtps.ssl.trust">*</prop>
    

    and changed the port to 587.

    Regards