Spring Mail身份验证错误

时间:2013-09-03 06:37:21

标签: java spring email authentication

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

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

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

有什么想法吗?

9 个答案:

答案 0 :(得分:26)

使用以下属性:

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"

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

答案 1 :(得分:1)

这对我有用:

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>

其次,您需要为不太安全的应用启用访问权限。

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

答案 2 :(得分:1)

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

  1. 登录Gmail。
  2. 转到Gmail security page
  3. 从那里找到2步验证。打开它。
  4. 选择“关闭”
  5. 在属性下面设置(使用):

    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>
    

    立即尝试。

答案 3 :(得分:1)

使用以下配置:

<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
  3. 访问网址
  4. 选择&#34;开启&#34;

答案 4 :(得分: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>

希望这会有所帮助!!

答案 5 :(得分:0)

mail.protocol=smtp
mail.smtp.auth=true
mail.port=25
mail.host=my.inbox.com
mail.username=xxx@inbox.com

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

答案 6 :(得分: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

答案 7 :(得分: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

答案 8 :(得分: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;
    }
}