Java Spring邮件身份验证错误

Java Spring邮件身份验证错误,java,spring,email,authentication,Java,Spring,Email,Authentication,我在我的项目中使用Spring3.1,为了发送电子邮件,我使用SpringMail。当我试图发送电子邮件时,我经常会出现以下错误: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: No authentication mechansims supported by bot

我在我的项目中使用Spring3.1,为了发送电子邮件,我使用SpringMail。当我试图发送电子邮件时,我经常会出现以下错误:

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
我的邮件服务器不需要用户名/密码,根据这一事实,此错误似乎很正常。但事实并非如此;在SpringMail的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.host=10.200.123.135 mail.transport.protocol=smtp


有什么想法吗?

这是因为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>
希望这有帮助

使用以下属性:

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>
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>
再次尝试运行代码,它应该可以工作

这对我很有用:

豆类共形

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

如果你已经在你的gmail帐户中启用了两步验证,你必须先禁用它。请按照以下步骤操作:

1.Login to Gmail. 
2.Access the URL as https://www.google.com/settings/security/lesssecureapps 
3.Select "Turn on"
登录到Gmail。 去。 从那里找到两步验证。打开它。 选择“关闭” 设置使用以下属性:

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

立即尝试。

使用以下配置:

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
<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>
并遵循以下程序:

登录到Gmail。 访问URL作为 选择“打开” 在EmailConfig类中


对这个问题有什么建议吗。你解决了这个问题吗?如果是,请建议我怎么做。查看Spring Boot扩展的问题我添加了lessecuredapps,但仍然给出相同的错误:S还帮助我在浏览器中确保您的gmail帐户只应登录,然后打开此帐户并单击Continue。这很有用,谢谢
<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>
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
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;
    }
}