Spring Mail javax.mail.AuthenticationFailedException: 334 VXNlcm5hbWU6

Environment:

JDK 1.5.X
Spring 3.0.5

Properties:

email.host=X.X.X.X
email.port=-1
email.auth=false
email.username=
email.password=

Config:

<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.auth}</prop>
    </props>
  </property>
</bean>

Code Snippet:

public class MailManager {

 private JavaMailSender mailSender;

 public void send(String[] recipients, String subject, String content,
   File[] files) {

   …………

   MimeMessage message = mailSender.createMimeMessage();

   // Multipart Message
   MimeMessageHelper helper = new MimeMessageHelper(message, true, charset);
   helper.setFrom(fromMail);
   helper.setTo(recipients);
   String[] ccMails = verifyRecipients(ccMail);
   if (ccMails.length > 0) {
     helper.setCc(ccMails);
   }

   helper.setSubject(subject);
   // Use HTML format
   helper.setText(content, true);

   // Attached Files
   if (files != null) {
     for (File file : files) {
       helper.addAttachment(file.getName(), file);
     }
   }
   mailSender.send(message);
   …………

  }
}

Result:

Even I set the auth property to false, the “javax.mail.AuthenticationFailedException” still occurs.

SMTP Logs

DEBUG SMTP: useEhlo true, useAuth false
...
DEBUG SMTP: Found extension "AUTH", arg "NTLM LOGIN"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Found extension "XSHADOW", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN
334 VXNlcm5hbWU6
...

Reason:

Spring Mail API treats empty user/password as empty String not Null, so it still handles the authentication process, even the auth property is set to false.

The SMTP server still does the authentication because user/password is not Null.

Solution:

Add Post-Process code before calling the send method

 JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
 
 if (StringUtils.isBlank(javaMailSender.getUsername())) {
   javaMailSender.setUsername(null);
 }
 if (StringUtils.isBlank(javaMailSender.getPassword())) {
   javaMailSender.setPassword(null);
 }

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