【转载】SpringBoot-配置发送邮件遇到的⼀些问题
前⾔:前⼀天调⽤163邮箱发送邮件还么有问题,今天再调⽤就各种发送不成功,害的我都关闭授权,还花了⼀⽑钱短信费重新开启授权,最后百度到了⼀篇⽂章,⾮常贴切,在此转载下。
本⼈遇到的错误代码是554,邮件主题和内容有敏感词汇(您的验证码是),或者被当作垃圾邮件,修改了⼀下主题和内容就OK了。
使⽤springboot向163邮箱发送邮件遇到⼀些问题,主要归为如下⼏点:
1.连接smtp.163异常,报错:java.UnknownHostException: smtp.163
2.⽤户权限不⾜,报错:javax.mail.AuthenticationFailedException: 550 User has no permission
3.认证错误,报错:javax.mail.AuthenticationFailedException: 535 Error: authentication failed
4.发送内容错误,报错:com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM
5.发送附件错误,报错:javax.mail.MessagingException: Empty multipart: multipart/related;
6.发送静态图⽚资源,查看邮件,图⽚显⽰不出来
问题1:
详细报错如下:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163 , 25; timeout
-1;
nested exception is:
java.UnknownHostException: smtp.163 . Failed messages:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163 , 25; timeout
-1;
nested exception is:
java.UnknownHostException: smtp.163 ; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163 , 25; timeoutspring framework
-1;
nested exception is:
java.UnknownHostException: smtp.163
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)
at javax.t(Service.java:366)
at org.springframework.mail.tTransport
(JavaMailSenderImpl.java:501)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at mail.service.MailSendService.sendInlineMail(MailSendService.java:111)
解决思路:
1.进⾏telnet检查,查看telent smtp.163 25,发现可以正常telent成功,说明⽹络没问题
2.查看配置是否正常,经查看,发现springboot的application.properties⽂件中spring.mail.host=smtp.16
3后⾯有空格,去掉空格即可
问题2:
详细报错如下:
org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is
javax.mail.AuthenticationFailedException: 550 User has no permission
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at mail.service.MailSendService.sendInlineMail(MailSendService.java:111)
st(TestMail.java:43)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
问题3:
详细报错如下:
javax.mail.AuthenticationFailedException: 535 Error: authentication failed
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at mail.service.MailSendService.sendInlineMail(MailSendService.java:111)
st(TestMail.java:43)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
解决思路:
1.163邮箱开通smtp服务后,javaMail发送邮箱要使⽤授权码作为登录密码
2.设置spring.mail.password=授权码
问题4:
详细报错如下:
org.springframework.mail.MailSendException: Failed messages:
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp11,D8CowACX7CmSHB5b3SrlCA--.26635S3 1528700051,please see mail.163/help/help_spam_16.htm?
ip=182.138.102.204&hostid=smtp11&time=1528700051
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:474)
~[spring-context-support-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
~[spring-context-support-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
~[spring-context-support-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at mail.service.MailSendService.sendSimpleMail(MailSendService.java:50) ~[classes/:na]
st(TestMail.java:36) [classes/:na]
flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80]
flect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_80]
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_80]
at flect.Method.invoke(Unknown Source) ~[na:1.7.0_80]
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
[junit-4.12.jar:4.12]
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
[junit-4.12.jar:4.12]
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
[junit-4.12.jar:4.12]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
问题5:
详细报错如下:
Caused by: javax.mail.MessagingException: Empty multipart: multipart/related;
boundary="----=_Part_2_1115361411.1528698722005"
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:548)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:81)
... 45 more
问题6:
⽆报错信息
解决思路:
1.检查代码,具体代码如下:
public boolean sendInlineMail(String fromPos, String toPos, String subject, FileSystemResource file)
{
MimeMessage msg = ateMimeMessage();
try
{
//MimeMessageHelper构造器,如果要发送附件邮件,必须指定multipart参数为true
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setFrom(fromPos);
helper.setTo(toPos);
helper.setSubject(subject);
helper.setText("<html><body>静态资源:<img src='cid:pic' /></body></html>", true);
helper.addInline("pic", file);
mailSender.send(msg);
}
catch (MessagingException e)
{
<("发送附件邮件失败.", e);
return false;
}
return true;
}
2.对代码进⾏了调整:将addInline⽅法要放在setText后⾯,进⾏测试发现正常了,可能原因是顺序如果不对,导致资源没有被正确引⽤。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论