Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Spring JavaMailSenderImpl for Gmail

Tags:

java

spring

gmail

I am trying to find the correct properties to use to connect to the Gmail SMTP sever using the JavaMailSenderImpl class.

Let me first say that I have tried the approach found here. This worked fine. But when I tried the configuration below that post with the exact same authentication information I received a javax.mail.AuthenticationFailedException.

My currently configuration looks like this.

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

Why am I still getting this javax.mail.AuthenticationFailedException if I know that my credentials are correct.

Update

Here is my updated code based on the answers below. I am still receiving the same exception.

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >     <property name="username" value="[email protected]" />     <property name="password" value="XXX" />     <property name="javaMailProperties">     <props>         <prop key="mail.smtp.from">[email protected]</prop>         <prop key="mail.smtp.user">[email protected]</prop>         <prop key="mail.smtp.password">XXX</prop>         <prop key="mail.smtp.host">smtp.gmail.com</prop>         <prop key="mail.smtp.port">587</prop>         <prop key="mail.smtp.auth">true</prop>         <prop key="mail.smtp.starttls.enable">true</prop>     </props>     </property> </bean> 
like image 636
Andrew Avatar asked Jan 06 '10 21:01

Andrew


People also ask

How do I automatically send email per day in spring boot?

You need to use Scheduler in your spring boot application to schedule your email task. The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces, respectively.


1 Answers

This worked for me:

        <property name="host"><value>smtp.gmail.com</value></property>         <property name="port"><value>587</value></property>         <property name="protocol"><value>smtp</value></property>         <property name="username"><value>${mail.username}</value></property>         <property name="password"><value>${mail.password}</value></property>         <property name="javaMailProperties">             <props>                 <prop key="mail.smtp.auth">true</prop>                 <prop key="mail.smtp.starttls.enable">true</prop>                 <prop key="mail.smtp.quitwait">false</prop>             </props>         </property> 

The real trick for me turned out to be that the "protocol" value has to be "smtp" (not "smtps").

like image 172
Ken Burcham Avatar answered Sep 30 '22 21:09

Ken Burcham