Ubuntu server issues with Spring Java Mail and Gmail SMTP

Hey everyone, I’m stuck with a weird problem. My email sending app works great on my Windows laptop, but it’s acting up on the Ubuntu production server. I keep getting this error:

java.lang.RuntimeException: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException

I’ve double-checked my Spring config file, and it looks okay to me. Here’s a simplified version:

<bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="password" value="mySecretPass"/>
    <property name="username" value="[email protected]"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.host">smtp.gmail.com</prop>
            <prop key="mail.smtp.port">465</prop>
            <prop key="mail.smtp.ssl.enable">true</prop>
        </props>
    </property>
</bean>

Any ideas why it’s failing on Ubuntu but not on Windows? Could it be a firewall thing? Or maybe I’m missing some Ubuntu-specific setup? Thanks for any help!

I encountered a similar issue when deploying my Spring application on Ubuntu. The problem was related to the SSL certificate validation. Try adding these properties to your configuration:

<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>

Also, ensure your Ubuntu server has the latest CA certificates installed. You can update them with:

sudo update-ca-certificates

If the issue persists, check your server’s outbound connections. Some hosting providers block outgoing SMTP traffic on certain ports for security reasons. You might need to contact your provider to allow outbound connections to Gmail’s SMTP servers.

heyy tom, sounds like a tricky one! have u tried using port 587 instead of 465? sometimes ubuntu can be picky bout ssl ports. also, check if ur server’s IP is whitelisted in gmail. if not, enable ‘less secure app access’ in ur gmail settings. hope this helps, mate!

I’ve run into this exact problem before on Ubuntu servers. One thing that often gets overlooked is the system time. If your server’s clock is off, it can cause SSL handshake failures with Gmail’s SMTP servers.

Try running ‘sudo ntpdate ntp.ubuntu.com’ to sync your system time. If that doesn’t work, you might need to adjust your timezone settings.

Another thing to check is your JDK version. Some older versions have issues with Gmail’s SSL certificates. Make sure you’re running an up-to-date version of Java on your Ubuntu server.

Lastly, if all else fails, you could try using OAuth2 instead of password authentication. It’s a bit more complex to set up, but it’s generally more reliable and secure. Let me know if you need help with that setup.