转载请注明出处:https://oldnoop.tech/c/181.html
引入依赖
spring-boot-starter-mail
配置mail
修改application.properties
spring.mail.host=smtp.tom.com
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=25000
spring.mail.default-encoding=UTF-8
编写邮件发送类
@Component
public class SimpleEmailSender {
//引入起步依赖,自动配置JavaMailSenderImpl
@Autowired
private JavaMailSenderImpl sender;
/**
*
* @param toAddres 收件人
* @param subject 邮件标题
* @param text 邮件内容
* @return
*/
public boolean sendEmail(String toAddres,String subject, String text){
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(sender.getUsername());
from.setPersonal("OLDNOOP系统", "UTF-8");
helper.setFrom(from);
helper.setTo(toAddres);
helper.setSubject(subject);
helper.setText(text, true);
sender.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
结合freemarker模板发送邮件内容
引入freemarker的起步依赖
spring-boot-starter-freemarker
编写邮件数据封装类
public class MailEntity implements Serializable{
private static final long serialVersionUID = 5390894818114117658L;
//发送人
private String from;
//发送者的显示名称
private String fromDisplayName;
//接收人
private List<String> to;
//抄送人
private List<String> cc;
//标题
private String subject;
//邮件内容
private String text;
//邮件内容的模板
private String template;
//邮件内容的模板接收的数据
private Map<String, Object> data;
//省略getter,setter,toString方法
}
编写邮件发送类代码
@Component
public class TemplateEmailSender {
//mail起步依赖,自动配置JavaMailSenderImpl
@Autowired
private JavaMailSenderImpl sender;
//freemarker起步依赖,自动配置FreeMarkerConfigurer
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
//发送邮件
public boolean send(MailEntity mail) {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(sender.getUsername());
from.setPersonal(mail.getFromDisplayName(), "UTF-8");
helper.setFrom(from);
if (mail.getTo() != null) {
helper.setTo(mail.getTo().toArray(new String[mail.getTo().size()]));
}
if (mail.getCc() != null) {
helper.setCc(mail.getCc().toArray(new String[mail.getCc().size()]));
}
helper.setSubject(mail.getSubject());
String text = mail.getText();
if (StringUtils.isEmpty(text)) {
text = getText(mail.getTemplate(), mail.getData());
}
helper.setText(text, true);
sender.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//获取邮件内容,使用freemarker模板渲染数据
private String getText(String templateFile, Map<String, Object> data) throws Exception {
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Template template = configuration.getTemplate(templateFile);
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
return text;
}
}
制作freemarker模板
比如:
制作模板mail.html,放于src/main/resources/templates下
发送邮件指定模板位置
检查邮箱