解决用spring发邮件验证失败问题

20 篇文章 0 订阅

用spring发邮件验证失败问题
 
[See nested exception: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException]
按照下述步骤,即可消除此异常信息

属性设置如下:

    <!-- 邮件认证实现 -->
    <bean id="smtpAuthenticator"
        class="yourpackge.service.SmtpAuthenticator">
        <constructor-arg value="yourname" ></constructor-arg>
        <constructor-arg value="yourpassword" ></constructor-arg>
    </bean>
    <!-- 邮件 mailSession -->
    <bean id="mailSession" class="javax.mail.Session"
        factory-method="getInstance">
        <constructor-arg>
            <props>
                <prop key="mail.smtp.auth">true</prop>
<!-- 如果mail服务器是ssl认证,则去掉这里的注释符号
                <prop key="mail.smtp.socketFactory.port">465</prop>
                <prop key="mail.smtp.socketFactory.class">
                    javax.net.ssl.SSLSocketFactory
                </prop>
                <prop key="mail.smtp.socketFactory.fallback">
                    false
                </prop>
-->
            </props>
        </constructor-arg>
        <constructor-arg ref="smtpAuthenticator" />
    </bean>
   
    <!--
        email发送
    -->
   
    <bean id="mailSender"
        class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="yoursmtpserver" />
        <property name="port" value="25" />
        <property name="session" ref="mailSession" />
    </bean>
    <!--
        email 模板
    -->
    <bean id="mailMessage"
        class="org.springframework.mail.SimpleMailMessage">
        <property name="from">
            <value><![CDATA[laoyin <youremailaddress>]]></value>
        </property>
        <property name="subject" value="You've got new Rantz!" />
        <property name="text">
            <value>
                <![CDATA[
            Someone's been ranting about you! Log in to RoadRantz.com or
            click on the link below to see what they had to say.
            http://weather.online.tj.cn/?state=%STATE%&plateNumber=%PLATE%
            ]]>
            </value>
        </property>
    </bean>
   
追加一个认证类,实现email的用户认证

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SmtpAuthenticator extends Authenticator {
    private String username;
    private String password;
   
    public SmtpAuthenticator(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
    }
}

使用spring 发送邮件,需要注意,一定要把mail.jar 和 activation.jar 放在classpath中。
spring 只是对javamail进行了包装。
如果你的服务器上已经配置了mailsession , 可以利用JNDI来取得你的mailsession设定。
例如:
<bean id="mailSession"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="mail/Session" />
<property name="resourceRef" value="true" />
</bean>
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="session" ref="mailSession" />
</bean>

 参考:
1. http://forum.springframework.org/showthread.php?t=33667
2. http://topic.csdn.net/u/20070920/21/c9d1c301-6ea1-4c58-b5fe-5118ee7d5f66.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值