6

我在我的项目中使用 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

我的邮件服务器不需要用户名/密码,根据这个事实,这个错误似乎很正常。但情况是;我找不到在 spring 邮件的 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>

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

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

有任何想法吗?

4

8 回答 8

35

使用这些属性:

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"

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

于 2015-06-11T21:42:55.587 回答
3

使用以下配置:

<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。
  2. 以https://www.google.com/settings/security/lesssecureapps访问 URL
  3. 选择“开启”
于 2017-05-22T11:47:20.903 回答
1

这对我有用:

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>

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

1. Login to Gmail. 
2. Access the URL as https://www.google.com/settings/security/lesssecureapps 
3. Select "Turn on"
于 2015-11-16T22:59:38.987 回答
1

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

  1. 登录到 Gmail。
  2. 转到Gmail 安全页面
  3. 从那里找到两步验证。打开它。
  4. 选择“关闭”

设置(使用)以下属性:

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>

现在试试。

于 2016-05-29T17:15:08.310 回答
0

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

希望这可以帮助 !!

于 2014-05-26T12:18:28.007 回答
0
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
于 2017-04-21T15:28:40.257 回答
0
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
于 2017-04-21T15:38:38.920 回答
0
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;
    }
}
于 2019-03-15T04:58:49.813 回答