概述
用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
最后
以上就是幽默猎豹为你收集整理的解决用spring发邮件验证失败问题 的全部内容,希望文章能够帮你解决解决用spring发邮件验证失败问题 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复