邮件发送
private static final String USER = "";
private static final String PASSWORD = "";
@Test
public void m5() {
StringBuilder content = new StringBuilder("<html><head><meta charset='utf-8'></head><body>" + 111 + "" +
",您好:<br>您有新的岗位人员需要验收,具体情况如下:<br>员工:" +1 + "<br>线体:" +1+ "<br>岗位:" + 1 );
content.append("<table border=\"5\" style=\"border:solid 2px #E8F2F9;font-size=14px;;font-size:25px;\">");
content.append("<tr style=\"background-color: #428BCA; color:#ffffff\"><th>姓名</th><th>线体</th><th>岗位</th><th>是否首次操作该岗位</th><th>培训方式</th><th>打卡日期</th><th>验收状态</th></tr>");
for (int i = 0; i < 1; i++) {
content.append("<tr style=\"font-size:20px;\">");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("<td style=\"text-align: center\">1</td>");
content.append("</tr>");
}
content.append("</table>");
content.append("</body></html>");
sendMail("发件人邮箱", content.toString(), "测试用");
System.out.println("发送成功");
}
public static boolean sendMail (String to, String text, String title){
try {
final Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", "IP地址/域名");
props.put("mail.user", USER);
props.put("mail.password", PASSWORD);
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession);
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(title);
message.setContent(text, "text/html;charset=UTF-8");
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}