相关依赖导入
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
Pet实体类
public class Pet {
private String name;
private String type;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
发送邮件工具类
public class EmailUtil {
//发送邮箱
public static String MAIN_CROP_EMAIL_USER = "aaa@xxx.com";
//授权密码(授权密码不是邮件的登录密码,是允许第三方登录时生成的授权密码)
public static String MAIN_CROP_EMAIL_PASS = "bbbbb";
//发件服务器
public static String HOST_NAME = "smtp.xxx.com";
//发送内容的格式
public static String CHARSET = "utf-8";
//发送服务器的端口号
public static int SMTP_PROT = 465;
//发送html格式的邮件
/**
*
* @param subject 标题
* @param htmlMsg html格式内容
* @param receiver 接收人邮箱
* @return
* @throws EmailException
*/
public static String sendHtmlMsgEmail(String subject,String htmlMsg,String receiver) throws EmailException {
HtmlEmail email = new HtmlEmail();
email.setHostName(HOST_NAME);
email.setSmtpPort(SMTP_PROT);
email.setCharset(CHARSET);
email.setAuthenticator(new DefaultAuthenticator(MAIN_CROP_EMAIL_USER, MAIN_CROP_EMAIL_PASS));
email.setSSLOnConnect(true);
email.setSubject(subject);
email.setFrom(MAIN_CROP_EMAIL_USER);
email.setHtmlMsg(htmlMsg);
email.addTo(receiver);
String res = email.send();
return res;
}
/**
*
* @param subject 标题
* @param msg 内容
* @param receiver 接收人邮箱
* @return
* @throws EmailException
*/
//普通邮件的发送
public static String sendSimpleMsgEmail(String subject,String msg,String receiver) throws EmailException{
SimpleEmail email = new SimpleEmail();
email.setHostName(HOST_NAME);
email.setSmtpPort(SMTP_PROT);
email.setCharset(CHARSET);
email.setAuthenticator(new DefaultAuthenticator(MAIN_CROP_EMAIL_USER, MAIN_CROP_EMAIL_PASS));
email.setSSLOnConnect(true);
email.setSubject(subject);
email.setFrom(MAIN_CROP_EMAIL_USER);
email.setMsg(msg);
email.addTo(receiver);
String res = email.send();
return res;
}
}
发送普通内容邮件测试类
public class TestSimpleEmail {
public static void main(String[] args) {
try{
EmailUtil.sendSimpleMsgEmail("普通邮件测试标题","普通邮件测试内容","aaa.@xxx.com");
}catch (EmailException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
结果截图:
发送html格式内容邮件测试类
public class TestHtmlEmail {
//html内容模板
public static String EMAIL_TEMPLATE_TABLE = "<html>\n" +
"<body>\n" +
"\n" +
"<h4 th:text=\"|Hi, ${customer}, these're your pets|\"></h4>\n" +
"<hr></hr>\n" +
"\n" +
"<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n" +
" <tr>\n" +
" <th>name</th>\n" +
" <th>type</th>\n" +
" <th>age</th>\n" +
" </tr>\n" +
" <tr th:each=\"p : ${pets}\">\n" +
" <td th:text=\"${p.name}\"></td>\n" +
" <td th:text=\"${p.type}\"></td>\n" +
" <td th:text=\"${p.age}\"></td>\n" +
" </tr>\n" +
"</table>\n" +
"\n" +
"</body>\n" +
"</html>";
public static void main(String[] args) {
List<Pet> petList = new ArrayList<>();
for(int i = 0;i <= 3;i++){
Pet pet = new Pet();
pet.setAge(1);
pet.setName("lucy");
pet.setType("dog");
petList.add(pet);
}
Context context = new Context();
context.setVariable("customer","Tom");
context.setVariable("pets",petList);
TemplateEngine templateEngine = new TemplateEngine();
String htmlMsg = templateEngine.process(EMAIL_TEMPLATE_TABLE, context);
String result = null;
try{
result = EmailUtil.sendHtmlMsgEmail("Pet table",htmlMsg,"aaa@xxx.com");
}catch (EmailException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
System.out.println(result);
}
}
结果截图:
注意:
1、接受邮箱服务器都需要开启smtp服务。
2、授权密码不是邮箱登录密码,是开启第三方发送邮箱的授权密码。