forked from eacdy/spring-boot-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -22,6 +28,8 @@ public class MailController { | |
private JavaMailSender javaMailSender; | ||
@Autowired | ||
private MailProperties mailProperties; | ||
@Autowired | ||
private Configuration freemarkerConfiguration; | ||
|
||
/** | ||
* 简单邮件测试 | ||
|
@@ -89,7 +97,7 @@ public String attach() throws MessagingException { | |
} | ||
|
||
/** | ||
* 内嵌附件的邮件测试 | ||
* 内联附件的邮件测试 | ||
* | ||
* @return success | ||
* @throws MessagingException | ||
|
@@ -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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>亲爱的${username}, 欢迎关注${event}</h1> |