开始测试前,要确保发邮件的服务器的smtp服务可用。
不然会抛出异常:
Sending the email to the following server failed : m.xxx.com:25
Caused by: javax.mail.AuthenticationFailedException: 334 NTLM supported
然后介绍下我的开发环境(context):目前用的开发框架是playframework,它帮忙封装了apache的mail工具类,代码如下:
public static void sendMail(SendMailDto sendMailDto){
if(sendMailDto!=null){
HtmlEmail email = new HtmlEmail();
email.setCharset("UTF-8");// 编码格式
try {
email.addTo(sendMailDto.accepterEmail);// 接收者
email.setFrom(sendMailDto.sender, sendMailDto.name);// 发送者,姓名
email.setSubject(sendMailDto.title);// 邮件标题
email.setMsg(sendMailDto.content);// 发送内容
Mail.send(email);
Logger.info("接收邮件: "+sendMailDto.accepterEmail+" 发送成功!");
Logger.info("发送邮件服务器: "+sendMailDto.sender);
Logger.info("发送邮件名: "+sendMailDto.name);
}catch (Exception e) {
Logger.info("邮件: "+sendMailDto.accepterEmail+" 发送失败!");
e.printStackTrace();
}
}
}
在配置文件中要申明:
mail.smtp.host=xxxx
mail.smtp.user=xxxx
mail.smt