Spring Mail身份验证错误

osh3o9ms  于 2023-03-28  发布在  Spring
关注(0)|答案(9)|浏览(191)

我在我的项目中使用spring 3.1,为了发送电子邮件,我使用spring mail。当我尝试发送电子邮件时,我总是得到这个错误:

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

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

<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在weblogic中,它的配置是:
mail.smtp.host10.200.123.135邮件传输协议=smtp
有什么想法吗

muk1a3rh

muk1a3rh1#

使用这些属性:

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

默认情况下,gmail不允许不太安全的应用程序进行身份验证。您需要在gmail帐户中打开该选项,以允许不太安全的应用程序进行身份验证。
请按照以下步骤操作:

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

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

lmyy7pcs

lmyy7pcs2#

使用以下配置:

<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="xxxx@gmail.com"/>
    <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>

并遵循以下程序:
1.登录Gmail。
1.以https://www.google.com/settings/security/lesssecureapps的身份访问URL
1.选择“打开”

zwghvu4y

zwghvu4y3#

这对我很有效:
豆类配置

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

其次,您需要打开安全性较低的应用程序的访问权限。

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

v64noz0r4#

如果您在Gmail帐户中启用了两步验证,则必须首先禁用该功能。请按照以下步骤操作:
1.登录Gmail。
1.转到Gmail安全页面。
1.从那里找到2步验证。打开它。
1.选择“关闭”
设置(使用)以下属性:

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

现在试试。

k97glaaz

k97glaaz5#

这是因为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>

还有像这样的false的身份验证,

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

希望这有帮助!!

erhoui1w

erhoui1w6#

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
smdnsysy

smdnsysy7#

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
yduiuuwa

yduiuuwa8#

spring.profiles.active=@active.mode@
spring.data.mongodb.database=karaoke-db
server.port=5813
base.host=@base.host@

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=yourEmail@gmail.com
spring.mail.password=yourEmailPassword
mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
mail.from.email=yourEmail@gmail.com
mail.debug=true

在您的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;
    }
}
lstz6jyr

lstz6jyr9#

首先,我试图通过在“安全”下的“Google帐户设置”中将“不太安全”选项更改为“开”来解决这个问题,但现在Google不再支持该选项。要解决这个问题,请在Google帐户设置的搜索栏上搜索“应用程序密码”。如果您没有找到“应用程序密码”,请在您的Google帐户上启用“两步验证”。

相关问题