Java Spring邮件认证错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18585682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:16:52  来源:igfitidea点击:

Spring Mail Authentication Error

javaspringemailauthentication

提问by Neron

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:

我在我的项目中使用 spring 3.1,为了发送电子邮件,我使用 spring 邮件。当我尝试发送电子邮件时,我会收到此错误:

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.

我的邮件服务器不需要用户名/密码,根据这一事实,这个错误似乎很正常。但情况是;我找不到在 spring 邮件的 org.springframework.mail.javamail.JavaMailSenderImpl 类中不传递用户名/密码的方法。

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:

邮件服务器在 weblogic 中,它的配置是:

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

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

Any ideas?

有任何想法吗?

采纳答案by sjaiswal

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.

默认情况下,gmail 不允许安全性较低的应用程序进行身份验证。您需要在您的 Gmail 帐户中打开该选项,以允许不太安全的应用程序进行身份验证。

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.

再次尝试运行您的代码,它应该可以工作。

回答by Santhosh

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

这是因为smtp. 要解决此问题,您需要从配置文件中删除用户名和密码。

<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 falselike this,

还有false像这样的认证,

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

Hope this helps !!

希望这可以帮助 !!

回答by Suyash Soni

mail.protocol=smtp
mail.smtp.auth=true
mail.port=25
mail.host=my.inbox.com
[email protected]

Try to use these settings. I also had similar issue when I was using port=587 and then I changed the port to 25 and it started working.

尝试使用这些设置。我在使用 port=587 时也遇到了类似的问题,然后我将端口更改为 25 并开始工作。

回答by Pradip Kharbuja

This worked for me:

这对我有用:

Bean Cofiguration

Bean 配置

<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"

回答by tk_

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

如果您在 Gmail 帐户中启用了两步验证,则必须先禁用它。请按照以下步骤操作:

  1. Login to Gmail.
  2. Go to Gmail security page.
  3. Find 2 step verification from there. open it.
  4. Select "Turn off"
  1. 登录 Gmail。
  2. 转到Gmail 安全页面
  3. 从那里找到两步验证。打开它。
  4. 选择“关闭”

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.

现在试试。

回答by GSK

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

回答by Shubham kapoor

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

回答by Sandip S.

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"
  1. 登录 Gmail。
  2. https://www.google.com/settings/security/lesssecureapps访问 URL
  3. 选择“开启”

回答by nobjta_9x_tq

[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

在你的 EmailConfig 类中

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;
    }
}

回答by KOUSHIK SARKAR

Hi I faced this problem and get Solution after did 2 Steps.

嗨,我遇到了这个问题,并在做了 2 个步骤后得到了解决方案。

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.username=--
spring.mail.password=--
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.debug=true
spring.mail.smtp.auth=true
spring.mail.smtp.starttls.enable=true

1. by activating the Lesssecureappshttps://www.google.com/settings/security/lesssecureapps

1. 通过激活 Lesssecureapps https://www.google.com/settings/security/lesssecureapps

2. UnlockCaptcha security byhttps://accounts.google.com/b/0/DisplayUnlockCaptcha

2. 通过https://accounts.google.com/b/0/DisplayUnlockCaptcha解锁验证码安全