javax.mail:未经身份验证的发送在 Transport.send() 内挂起

javax.mail: Sending without authentication hangs inside Transport.send()

我正在尝试使用本地只发送 Postfix 服务器从应用程序发送错误报告。服务器工作正常(我已经使用 telnet 和邮件对其进行了测试)但是我无法让下面的代码工作:

Properties props = new Properties();
props.put("mail.smtp.host",host);                   // "localhost"
props.put("mail.smtp.port",port);                   // "25"
props.put("mail.smtp.auth",auth);                   // false
props.put("mail.smtp.starttls.enable",tls);         // false
props.put("mail.smtp.ssl.enable",ssl);              // false
props.put("mail.smtp.sendpartial",true);

Session session = Session.getDefaultInstance(props);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject(subject);
msg.setContent(content,"text/plain");

Transport.send(msg);

我一直追踪到对 send() 的最终调用,它就在那个时候挂起——它从未从调用中 returns。

如果我将“mail.smtp.auth”设置为 true 并用以下代码替换对 Transport.send() 的调用:

Transport transport = s.getTransport("smtp");
transport.connect(host,Integer.parseInt(port,10),"foo","bar");
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
transport.close();

然后它在对 connect() 的调用中挂起。如果我在对 connect() 的调用中将“mail.smtp.auth”设置为 false 并将用户名和密码设置为 null,则与 true 相同。

在 /var/log/mail.log 中我看到了这个:

connect from localhost[127.0.0.1]

在我杀死挂起的进程后:

lost connection after CONNECT from localhost[127.0.0.1]
disconnect from localhost[127.0.0.1] commands=0/0

谁能看出我做错了什么?

问题原来是用户“postfix”被iptables 屏蔽了。为 postfix 添加 ACCEPT 规则解决了问题。