- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>com.sun.mailgroupId>
- <artifactId>javax.mailartifactId>
- <version>1.6.2version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-context-supportartifactId>
- <version>5.3.20version>
- dependency>
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- import javax.mail.util.ByteArrayDataSource;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
-
- public class App {
- public static void main(String[] args) {
- Properties property = new Properties();
- property.put("mail.smtp.auth", "true");
- // 邮件发送超时设置
- property.put("mail.smtp.connectiontimeout", 3000);
- property.put("mail.smtp.timeout", 30000);
- property.put("mail.smtp.host", "这里是邮箱服务器地址");
- Authenticator authenticator = new MailAuthenticator("邮箱账号", "密码");
-
- try {
- String sendto = "XXX@qq.com"; // 接收方
- Session mailSession = Session.getDefaultInstance(property, authenticator);
- MimeMessage message = new MimeMessage(mailSession);
- String nick = "";
- try {
- nick = MimeUtility.encodeText("主题");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- message.setFrom(new InternetAddress(nick + " <" + "邮箱账号" + ">"));
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(sendto));
- message.setSubject("测试邮件");
- Multipart multipart = new MimeMultipart();
- BodyPart contentPart = new MimeBodyPart();
- contentPart.setContent("测试主体", "text/html;charset=UTF-8");
- multipart.addBodyPart(contentPart);
- message.setContent(multipart);
- // 这个是附件,放在本地
- InputStream inputStream = App.class.getResourceAsStream("/fapiao.pdf");
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- byte[] buffer;
- try {
- buffer = new byte[inputStream.available()];
- int n = 0;
- while (-1 != (n = inputStream.read(buffer))) {
- output.write(buffer, 0, n);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- byte[] content = output.toByteArray();
- // 这里模拟添加三个pdf附件
- for(int i=0; i<3; i++) {
- BodyPart attachmentBodyPart = new MimeBodyPart();
- DataSource source = new ByteArrayDataSource(content, "application/pdf");
- attachmentBodyPart.setDataHandler(new DataHandler(source));
- try {
- attachmentBodyPart.setFileName(MimeUtility.encodeWord("mypdf.pdf"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- multipart.addBodyPart(attachmentBodyPart);
- }
- Long start = System.currentTimeMillis();
- System.out.println("向"+sendto+"发送邮件,附带附件,总大小:"+message.getSize());
- Transport.send(message);
- System.out.println("邮件发送成功,耗时="+(System.currentTimeMillis()-start)+"ms");
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- return;
- }
- }
在本地测试,几秒就收到了邮箱,但是在服务器发送,就20-30s左右才可以收到。在生产环境,有大量邮件发送堆积,直接导致发送失败。
报错信息:
Could not connect to SMTP host: mail.bjgas.com, port: 25, response: 421]
或
邮件发送异常EOF
通过跟踪本地邮件发送程序源码,最终发现是服务器没有在hosts配置本机的主机名导致的。







通过上面代码跟踪发现,在执行 ehlo xxx 时,xxx的值默认会调 getlocalHost() ,因为刚开始进来localHostName为空,所以去调用 InetAddress.getLocalHost()方法。又通过InetAddress.getAddressesFromNameService(local, null); 方法获取服务器本地ip。
最终调用 nameService.lookupAllHostAddr(host) 方法,当 hosts 文件中没有添加主机名时,getaddrinfo 调用返回错误码,此时 jdk 会转而调用 lookupIfLocalhost 方法,它内部调用了操作系统的 getifaddrs 方法,以获取本机所有 ip 地址(我这边光这一步就花费10s左右);当获取到所有的ip地址后,进行一系列解析处理,最终导致发送邮件的时候发送慢、超时或其他报错。
当 hosts 文件中没有添加主机名时,会返回本机所有的 ip 地址;
当 hosts 文件中添加主机名后,只会返回配置的 127.0.01 的 ip 地址;
- 127.0.0.1 hostname
- ip/域名 hostname
- property.put("mail.smtp.localhost","主机名称");
- property.put("mail.smtp.localaddress","主机ip");

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