Last Updated: February 25, 2016
·
3.105K
· corleonis

Configure Spring to send email with GMail SMTP

Sending emails with Spring could be quite straight forward just need to configure a bean and JavaMail dependencies. First thing first:

1. Insert mail dependencies

File : pom.xml

<dependencies>
    ... 
    <!-- Java Mail API -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.3</version>
    </dependency>
    ...
</dependencies>

2. Bean configuration file

<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="username" />
    <property name="password" value="password" />

    <property name="javaMailProperties">
       <props>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.starttls.enable">true</prop>
           </props>
    </property>
</bean>

<bean id="mailMail" class="com.corleonis.common.MailMail">
        <property name="mailSender" ref="mailSender" />
</bean>

That's it straight forward now you can set and send emails from your application.

http://yysource.com