大家好,我是烤鸭:
阿里云邮件推送服务报错。当你也使用阿里云的邮件推送服务提示上面这个错误的话,我先告诉你原因和目前能想到的解决方案。
解决思路:
1. 换企业邮箱,阿里的上限15M,网易的不知道,反正30M没问题。
2. 把附件上传到oss上,邮件发送oss地址,让用户自己去下载。
3. 附件内容拆分,分成几个邮件发出。
由于这个问题花了些时间,也记录一下过程。
1 问题复现
报错内容:
javax.mail.MessagingException: IOException while sending message;nested exception is:java.net.SocketException: Connection reset by peer: socket write errorat com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1167)at javax.mail.Transport.send0(Transport.java:195)at javax.mail.Transport.send(Transport.java:124)at com.iruidou.job.job.db.ruidou.sendEmails.utils.SendMail.sendVerifyXlsxFile(SendMail.java:380)at com.iruidou.job.service.preauthorize.SendHbPreauthorizeDeductDataService.sendHbPreauthorizeDeductDataEmail(SendHbPreauthorizeDeductDataService.java:99)at com.iruidou.job.test.SpringTransactionalContextTests.test11(SpringTransactionalContextTests.java:144)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
出现这个问题第一时间想到的是socket 超时,网上查了类似的资料,关键字 :javax.mail 邮件发送 阻塞 超时。
这篇写的挺好的:
https://www.cnblogs.com/Java3y/p/11471036.html
2.修改之后报错同样,设置debug模式
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.auth", "true");
// 发件人的账号
props.put("mail.user", emailSenderAccount);
// 访问SMTP服务时需要提供的密码
props.put("mail.password", emailSenderPassword);
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
props.put("mail.smtp.host", "smtpdm.aliyun.com");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}
};
//2、创建定义整个应用程序所需的环境信息的 Session 对象
Session session = Session.getInstance(props,authenticator);
//设置调试信息在控制台打印出来
session.setDebug(true);
如果出现以下的问题:
DEBUG SMTP: IOException while sending, closing, THROW:
java.io.IOException: javax.mail.MessagingException: No MimeBodyPart contentat com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105)at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:889)at javax.activation.DataHandler.writeTo(DataHandler.java:317)at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1485)at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773)at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)at com.etc.email.EmailTest.sendEmailTest(EmailTest.java:104)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
代码肯定哪有问题,用下面代码,亲测可用。
https://www.cnblogs.com/eason-d/p/8039347.html
如果出现 421 的返回码:
DATA
421 4.4.5 HL:ICC .For more information, please visit https://feedback.qiye.163.com/bounce/ABFBVSMHL1UjWg5deEB4cQYGBS0%2FeFQnbkxEXUch
DEBUG SMTP: got response code 421, with response: 421 4.4.5 HL:ICC .For more information, please visit https://feedback.qiye.163.com/bounce/ABFBVSMHL1UjWg5deEB4cQYGBS0%2FeFQnbkxEXUchRSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 421 4.4.5 HL:ICC .For more information, please visit https://feedback.qiye.163.com/bounce/ABFBVSMHL1UjWg5deEB4cQYGBS0%2FeFQnbkxEXUchat com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)at com.sun.mail.smtp.SMTPTransport.data(SMTPTransport.java:1876)at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)at com.etc.email.EmailTest.sendEmailTest(EmailTest.java:104)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
•421 HL:REP 该IP发送行为异常,存在接收者大量不存在情况,被临时禁止连接。请检查是否有用户发送病毒或者垃圾邮件,并核对发送列表有效性;
•421 HL:ICC 该IP同时并发连接数过大,超过了网易的限制,被临时禁止连接。请检查是否有用户发送病毒或者垃圾邮件,并降低IP并发连接数量;
•421 HL:IFC 该IP短期内发送了大量信件,超过了网易的限制,被临时禁止连接。请检查是否有用户发送病毒或者垃圾邮件,并降低发送频率;
•421 HL:MEP 该IP发送行为异常,存在大量伪造发送域域名行为,被临时禁止连接。请检查是否有用户发送病毒或者垃圾邮件,并使用真实有效的域名发送;
更多错误码的介绍:
https://blog.csdn.net/wangyanming123/article/details/52734334
可能是同一时间发送次数过多,被拦截了,换个ip或者等一会再试。(用网易的企业邮箱会有这个问题,阿里云的没事)