Neron
Neron

Reputation: 1578

Spring Mail Authentication Error

I am using spring 3.1 in my project and in order to send email, I am using spring mail. When I am trying to send an email, I aıways get this error:

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client

My mail server does not require a username/ password and this error seems normal according to this fact. But the case is; I could not find a way to not to pass username/password in spring mail's org.springframework.mail.javamail.JavaMailSenderImpl class.

My config is :

<jee:jndi-lookup id="mailSession" jndi-name="${abc.app.mailSession}" cache="true"/>

    <bean id="jndiMailSender" class="com.abc.service.mail.JndiJavaMailService">
        <property name="session" ref="mailSession"/>
        <property name="defaultEncoding" value="${mail.defaultEncoding}"/>
        <property name="username" value="${abc.mail.username}"/>
        <property name="password" value="${abc.mail.password}"/>
        <property name="mailMasterAdress" value="${abc.mail.mailMasterAdress}"/>
    </bean>

Mailserver is in weblogic and it's configs are:

mail.smtp.host=10.200.123.135 mail.transport.protocol=smtp

Any ideas?

Upvotes: 6

Views: 54220

Answers (9)

First, I tried to solve this problem by changing the "less secure" option to ON on Google Account Settings under Security, but now Google hasn't supported that option anymore. To fix this, get an 'app password' by searching on the search bar on Google Account Settings. If you don't find the 'app password', enable '2-Step Verification' on your Google Account.

Upvotes: 1

nobjta_9x_tq
nobjta_9x_tq

Reputation: 1241

[email protected]@
spring.data.mongodb.database=karaoke-db
server.port=5813
[email protected]@

spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=yourEmailPassword
mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
[email protected]
mail.debug=true

And in your EmailConfig class

package com.tlcreativeltd.karaoke.config.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.util.Properties;

@Configuration
@PropertySource("classpath:application.properties")
public class MailConfig {
    @Value("${spring.mail.host}")
    private String host;

    @Value("${spring.mail.port}")
    private int port;

    @Value("${spring.mail.username}")
    private String userName;

    @Value("${spring.mail.password}")
    private String passwd;

    @Value("${mail.transport.protocol}")
    private String protocol;

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

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);

        mailSender.setUsername(userName);
        mailSender.setPassword(passwd);

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", protocol);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.from.email", from);
        props.put("mail.debug", "true");

        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, passwd);
            }
        };

        Session session = Session.getInstance(props, auth);
        mailSender.setSession(session);

        return mailSender;
    }
}

Upvotes: 0

Sandip S.
Sandip S.

Reputation: 628

Use below configuration:

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

and follow below procedure:

  1. Login to Gmail.
  2. Access the URL as https://www.google.com/settings/security/lesssecureapps
  3. Select "Turn on"

Upvotes: 3

Shubham kapoor
Shubham kapoor

Reputation: 392

using Spring 1.2.4
<sun-mail.version>1.5.3</sun-mail.version>
spring.mail.host=smtp.gmail.com
spring.mail.username=*****@***.co.xx
spring.mail.password=.......
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable = true


all this  and goto https://myaccount.google.com/security of the configured account and 
Allow less secure apps: ON

Upvotes: 0

GSK
GSK

Reputation: 563

using Spring 1.2.4
<sun-mail.version>1.5.3</sun-mail.version>
spring.mail.host=smtp.gmail.com
spring.mail.username=*****@***.co.xx
spring.mail.password=.......
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
#spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable = true


all this  and goto https://myaccount.google.com/security of the configured account and 
Allow less secure apps: ON
Worked for Me

Upvotes: 0

tk_
tk_

Reputation: 17318

If you have enabled 2 step verification in your gmail account you have to disable that first. Please follow these steps:

  1. Login to Gmail.
  2. Go to Gmail security page.
  3. Find 2 step verification from there. open it.
  4. Select "Turn off"

Set(use) below properties:

mail.host=smtp.gmail.com
mail.port=587
mail.username=<[email protected]>
mail.password=<password>
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.from.email=<[email protected]>

Try now.

Upvotes: 1

Pradip Kharbuja
Pradip Kharbuja

Reputation: 3532

This worked for me:

Bean Cofiguration

<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="<!--Gmail ID -->" />
    <property name="password" value="<!-- Gmail Password -->" />

    <!-- The name of the property, following JavaBean naming conventions -->
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>

Secondly, you need to turn on the access for less secure apps.

1. Login to Gmail. 
2. Access the URL as https://www.google.com/settings/security/lesssecureapps 
3. Select "Turn on"

Upvotes: 1

sjaiswal
sjaiswal

Reputation: 498

Use these properties:

mail.host=smtp.gmail.com
mail.port=587
mail.username=<[email protected]>
mail.password=<gmail-password>
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.from.email=<[email protected]>

By default, gmail does not allow less secure apps to get authenticated. You need to turn on the option in you gmail account to allow less secure apps to get authenticated.

Follow these steps:

1.Login to Gmail. 
2.Access the URL as https://www.google.com/settings/security/lesssecureapps 
3.Select "Turn on"

Try running your code again, it should work.

Upvotes: 40

Santhosh
Santhosh

Reputation: 8187

It is because authentication not supported by the smtp . to resolve this you need to remove the username and password from the config file .

<jee:jndi-lookup id="mailSession" jndi-name="${abc.app.mailSession}" cache="true"/>

<bean id="jndiMailSender" class="com.abc.service.mail.JndiJavaMailService">
    <property name="session" ref="mailSession"/>
    <property name="defaultEncoding" value="${mail.defaultEncoding}"/>
    <property name="mailMasterAdress" value="${abc.mail.mailMasterAdress}"/>
</bean>

And also authentication to false like this,

<prop key="mail.smtp.auth">false</prop>

Hope this helps !!

Upvotes: 0

Related Questions