Skip to content

Commit

Permalink
spring boot发送邮件
Browse files Browse the repository at this point in the history
  • Loading branch information
eacdy authored and limu.zl committed Mar 21, 2019
1 parent d3193db commit 76849d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions spring-boot-mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.itmuch.email;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @author itmuch.com
Expand All @@ -22,6 +28,8 @@ public class MailController {
private JavaMailSender javaMailSender;
@Autowired
private MailProperties mailProperties;
@Autowired
private Configuration freemarkerConfiguration;

/**
* 简单邮件测试
Expand Down Expand Up @@ -89,7 +97,7 @@ public String attach() throws MessagingException {
}

/**
* 内嵌附件的邮件测试
* 内联附件的邮件测试
*
* @return success
* @throws MessagingException
Expand All @@ -109,4 +117,33 @@ public String inlineAttach() throws MessagingException {
this.javaMailSender.send(message);
return "success";
}

/**
* 内联附件的邮件测试
*
* @return success
* @throws MessagingException
*/
@GetMapping("/freemarker")
public String freemarker() throws MessagingException, IOException, TemplateException {
MimeMessage message = this.javaMailSender.createMimeMessage();
// 第二个参数表示是否开启multipart模式
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(this.mailProperties.getUsername());
messageHelper.setTo("[email protected]");
messageHelper.setSubject("基于freemarker模板的邮件测试");

Map<String, Object> model = new HashMap<>();
model.put("username", "itmuch");
model.put("event", "IT牧场大事件");

String content = FreeMarkerTemplateUtils.processTemplateIntoString(
this.freemarkerConfiguration.getTemplate("mail.ftl"), model);

// 第二个参数表示是否html,设为true
messageHelper.setText(content, true);

this.javaMailSender.send(message);
return "success";
}
}
1 change: 1 addition & 0 deletions spring-boot-mail/src/main/resources/templates/mail.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>亲爱的${username}, 欢迎关注${event}</h1>

0 comments on commit 76849d1

Please sign in to comment.