源代码
// Common variables
String host = "your_smtp_server";
String from = "from_address";
String to = "to_address";
// Set properties
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
// Get session
Session session = Session.getInstance(props);
try {
// Instantiate a message
Message msg = new MimeMessage(session);
// Set the FROM message
msg.setFrom(new InternetAddress(from));
// The recipients can be more than one so we use an array but you can
// use 'new InternetAddress(to)' for only one address.
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// Set the message subject and date we sent it.
msg.setSubject("Email from JavaMail test");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is the text for this simple demo using JavaMail.");
// Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
解析
*注意加粗部分,翻译过来就是“默认时间无穷大”,总之一句话:你要不配置这几个参数,邮箱发送的等待时间可能是无穷大,出现异常后(比如邮箱服务器繁忙)客户端有可能永远在等待往服务器读写消息。这里涉及到跟服务器间的socket交互。*如下图,JavaMail在socket读写过程中有可能无限等待下去
解决方法
把JavaMail重构成异步,避免阻塞核心业务逻辑。
为JavaMail设置超时时间
Properties props = new Properties();
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
// Get session
Session session = Session.getInstance(props);