HtmlEmail email = new HtmlEmail();
try {
// 设置邮件服务器的名称
email.setHostName("smtp.163.com");
// 设置发送账户和密码
email.setAuthentication("账号","密码");
// 设置邮件发送者
email.setFrom("账号");
// 设置邮件编码
email.setCharset("UTF-8");
// 设置邮件主题
email.setSubject("邮件主题");
// 设置邮件内容
email.setHtmlMsg("较好"); // 会有异常
// 设置邮件发送时间
email.setSentDate(new Date());
// 设置收件人
email.addTo("邮箱账号");
email.send();
}catch (Exception e){
System.out.println(e.getMessage());
}
pom 坐标:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3.1</version>
</dependency>