AuthenticationFailedException When Using Spring Mail with GMail SMTP

Situation

When using GMail SMTP to test send mail function, an AuthenticationFailedException occurs.

Environment

Maven: 4.0.0

Weblogic: 10.3.6

Spring Mail: 3.1.0

GMail SMTP: smtp.gmail.com:25

Solution

Step 1. Login Gmail→My Account→Connected apps & sites→Allow less secure apps→On (https://www.google.com/settings/security/lesssecureapps). If set to Off, an exception like “org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException…” may occur.

Step 2. Create a Maven web-app project→pom.xml

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${spring.version}</version>
</dependency>
<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4</version>
</dependency>
......
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <fork>true</fork>
        <verbose>true</verbose>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>${project.build.sourceEncoding}</encoding>
      </configuration>
    </plugin>
    ......
  <plugins>
</build>

Step 3. Create an email property

mail.properties

##### Mail Configuration #####
email.host=smtp.gmail.com
email.port=25
email.auth=true
email.starttls=true
email.debug=true
email.username=[your gmail]
email.password=[pwd]
email.from=[your gmail]
# Comma-Separated Values
email.to=[recipients]

beans-email-config.xml (whatever you like)

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="${email.host}" />
  <property name="port" value="${email.port}" />
  <property name="username" value="${email.username}" />
  <property name="password" value="${email.password}" />
  <property name="javaMailProperties">
    <props>
      <prop key="mail.smtp.auth">${email.auth}</prop>
      <prop key="mail.smtp.starttls.enable">${email.starttls}</prop>
      <prop key="mail.debug">${email.debug}</prop>
    </props>
  </property>
</bean>

Step 4. Bind the email property to web.xml

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath*:config/beans-*-config.xml
  </param-value>
</context-param>

Step 5. Do not set “javax.net.ssl.trustStore”. If you set it wrong, the mail utility may not work properly. An exception like “javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty…” may occur.

Step 6. Code snippet

@Service("MailManager")
public class MailManager {

  @Autowired
  private JavaMailSender mailSender;

  @Value("${email.from}")
  private String fromMail;

  @Value("#{'${email.to}'.split(',')}")
  private String[] toMail;

  public void send(String[] recipients, MailContent mailContent, File[] files) {
    try {
      ......
      JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
    
      // Multipart Message
      MimeMessageHelper helper = new MimeMessageHelper(message, true, CHARSET);
      helper.setFrom(fromMail);
      helper.setTo(recipients);

      helper.setSubject(mailContent.getSubject());
      helper.setText(mailContent.getContent(), false);
      ......
      helper.addAttachment(file.getName(), file);
      ......
      mailSender.send(message);

    } catch (Exception e) {
      ......
    }
  }

 

Reference

http://howtodoinjava.com/spring/spring-core/send-email-with-spring-javamailsenderimpl-example/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s