• 使用java mail SMTPTransport发送邮箱,本地秒到,一上服务器就20-30s左右,生产环境直接发送失败。


    一、代码

    pom文件

    1. <dependency>
    2. <groupId>junitgroupId>
    3. <artifactId>junitartifactId>
    4. <version>4.11version>
    5. <scope>testscope>
    6. dependency>
    7. <dependency>
    8. <groupId>com.sun.mailgroupId>
    9. <artifactId>javax.mailartifactId>
    10. <version>1.6.2version>
    11. dependency>
    12. <dependency>
    13. <groupId>org.springframeworkgroupId>
    14. <artifactId>spring-context-supportartifactId>
    15. <version>5.3.20version>
    16. dependency>

    发送案例

    1. import javax.activation.DataHandler;
    2. import javax.activation.DataSource;
    3. import javax.mail.*;
    4. import javax.mail.internet.InternetAddress;
    5. import javax.mail.internet.MimeBodyPart;
    6. import javax.mail.internet.MimeMessage;
    7. import javax.mail.internet.MimeMultipart;
    8. import javax.mail.internet.MimeUtility;
    9. import javax.mail.util.ByteArrayDataSource;
    10. import java.io.ByteArrayOutputStream;
    11. import java.io.IOException;
    12. import java.io.InputStream;
    13. import java.io.UnsupportedEncodingException;
    14. import java.util.Properties;
    15. public class App {
    16. public static void main(String[] args) {
    17. Properties property = new Properties();
    18. property.put("mail.smtp.auth", "true");
    19. // 邮件发送超时设置
    20. property.put("mail.smtp.connectiontimeout", 3000);
    21. property.put("mail.smtp.timeout", 30000);
    22. property.put("mail.smtp.host", "这里是邮箱服务器地址");
    23. Authenticator authenticator = new MailAuthenticator("邮箱账号", "密码");
    24. try {
    25. String sendto = "XXX@qq.com"; // 接收方
    26. Session mailSession = Session.getDefaultInstance(property, authenticator);
    27. MimeMessage message = new MimeMessage(mailSession);
    28. String nick = "";
    29. try {
    30. nick = MimeUtility.encodeText("主题");
    31. } catch (UnsupportedEncodingException e) {
    32. e.printStackTrace();
    33. }
    34. message.setFrom(new InternetAddress(nick + " <" + "邮箱账号" + ">"));
    35. message.setRecipient(Message.RecipientType.TO, new InternetAddress(sendto));
    36. message.setSubject("测试邮件");
    37. Multipart multipart = new MimeMultipart();
    38. BodyPart contentPart = new MimeBodyPart();
    39. contentPart.setContent("测试主体", "text/html;charset=UTF-8");
    40. multipart.addBodyPart(contentPart);
    41. message.setContent(multipart);
    42. // 这个是附件,放在本地
    43. InputStream inputStream = App.class.getResourceAsStream("/fapiao.pdf");
    44. ByteArrayOutputStream output = new ByteArrayOutputStream();
    45. byte[] buffer;
    46. try {
    47. buffer = new byte[inputStream.available()];
    48. int n = 0;
    49. while (-1 != (n = inputStream.read(buffer))) {
    50. output.write(buffer, 0, n);
    51. }
    52. } catch (IOException e) {
    53. e.printStackTrace();
    54. }
    55. byte[] content = output.toByteArray();
    56. // 这里模拟添加三个pdf附件
    57. for(int i=0; i<3; i++) {
    58. BodyPart attachmentBodyPart = new MimeBodyPart();
    59. DataSource source = new ByteArrayDataSource(content, "application/pdf");
    60. attachmentBodyPart.setDataHandler(new DataHandler(source));
    61. try {
    62. attachmentBodyPart.setFileName(MimeUtility.encodeWord("mypdf.pdf"));
    63. } catch (UnsupportedEncodingException e) {
    64. e.printStackTrace();
    65. }
    66. multipart.addBodyPart(attachmentBodyPart);
    67. }
    68. Long start = System.currentTimeMillis();
    69. System.out.println("向"+sendto+"发送邮件,附带附件,总大小:"+message.getSize());
    70. Transport.send(message);
    71. System.out.println("邮件发送成功,耗时="+(System.currentTimeMillis()-start)+"ms");
    72. } catch (MessagingException e) {
    73. e.printStackTrace();
    74. }
    75. return;
    76. }
    77. }

    二、现象

    在本地测试,几秒就收到了邮箱,但是在服务器发送,就20-30s左右才可以收到。在生产环境,有大量邮件发送堆积,直接导致发送失败。

    报错信息:

    Could not connect to SMTP host: mail.bjgas.com, port: 25, response: 421]

    邮件发送异常EOF

     三、排查

    1、跟踪 

    通过跟踪本地邮件发送程序源码,最终发现是服务器没有在hosts配置本机的主机名导致的。

     

    通过上面代码跟踪发现,在执行 ehlo xxx 时,xxx的值默认会调 getlocalHost() ,因为刚开始进来localHostName为空,所以去调用 InetAddress.getLocalHost()方法。又通过InetAddress.getAddressesFromNameService(local, null); 方法获取服务器本地ip。

    最终调用 nameService.lookupAllHostAddr(host) 方法,当 hosts 文件中没有添加主机名时,getaddrinfo 调用返回错误码,此时 jdk 会转而调用 lookupIfLocalhost 方法,它内部调用了操作系统的 getifaddrs 方法,以获取本机所有 ip 地址(我这边光这一步就花费10s左右);当获取到所有的ip地址后,进行一系列解析处理,最终导致发送邮件的时候发送慢、超时或其他报错。

    2. 总结:

    当 hosts 文件中没有添加主机名时,会返回本机所有的 ip 地址;

    当 hosts 文件中添加主机名后,只会返回配置的 127.0.01 的 ip 地址;

    四、解决方案

    1、在本地的/etc/hosts文件配置本地ip或域名与主机名对应;(建议都配置)

    1. 127.0.0.1 hostname
    2. ip/域名 hostname

    2、在代码里写入加入配置,不让它默认调getlocalhost()方法

    1. property.put("mail.smtp.localhost","主机名称");
    2. property.put("mail.smtp.localaddress","主机ip");

    方法2还没有验证,请谨慎使用。 

  • 相关阅读:
    网络练习题带答案
    3分钟了解Kfaka
    阿里云国际版CDN-阿里云CDN是什么?阿里云折扣怎么买
    rsync远程同步
    企业架构LNMP学习笔记36
    《软件方法》自测题解析-006:误以为是业务建模
    已解决fatal error: Python.h: No such file or directory
    Java对象初始化
    React源码之Fiber架构
    Linux 进程创建,进程状态,优先级
  • 原文地址:https://blog.csdn.net/m0_47743175/article/details/133973809