加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

解析用spring发邮件验证没成功问题

发布时间:2023-07-21 13:59:04 所属栏目:教程 来源:
导读:用spring发邮件验证失败问题

[See nested exception: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException]

按照
用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>
 
 
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!