spring bootのメール送信
2092 ワード
pomパッケージ配置
controlerのコード
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-freemarker
テンプレートエンジンの設定spring:
freemarker:
template-loader-path: classpath:/templates/
メールサーバの設定spring:
mail
host: smtp.qq.com
username: [email protected]
password: cumhxmicujosbjii # POP3
default-encoding: UTF-8
簡単なテキストメール@Override
public void sendSimpleMail(String to, String subject, String content) throws MailException {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); //
message.setTo(to); //
message.setSubject(subject); //
message.setText(content); //
mailSender.send(message);
}
写真付きメールを送信します.controlerのコード
@ApiOperation(value = "html " , notes="html ")
@GetMapping("/testHtml")
public ResultVo testHtml() throws IOException, TemplateException, MessagingException {
Map model = new HashMap();
model.put("UserName", "yao");
Template template = configuration.getTemplate("welcome.ftl");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
emailService.sendHtmlMail("[email protected]","html ",html);
return ResultUtils.success();
}
serviceのコード@Override
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
//true multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}