用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>
用spring发邮件验证失败问题
原创
©著作权归作者所有:来自51CTO博客作者gundumw100的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章