Springboot發送郵件工具類,可帶附件,可以模版形式發送

發送郵件

  • pom.xml添加引用
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- 支持發送郵件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>
  • 配置發送郵件的application
spring
  mail:
    default-encoding: UTF-8
    host: smtp.qq.com
    username: [email protected]
    password: 1234567890
    port: 22
    protocol: smtp
    properties:
      mail:
        smtp:
          ssl:
            enable: true
          socketFactory:
            port: 465
            class: javax.net.ssl.SSLSocketFactory
  • 編寫郵件的工具類
@Service
public class EmailUtil {

    @Autowired
    private JavaMailSender mailSender;
    /**
     * 用來發送模版郵件
     */
    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String from;

    @Async
    public void sendEmail(Context context, String templateName, String to, String [] cc,
                          String subject, String text, List<String> attachmentList){
        try {
        	// 解決附件名稱過長導致的附件名稱亂碼問題
            System.setProperty("mail.mime.splitlongparameters", "false");
            // 定義郵件信息
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper;
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            if(cc != null && cc.length > 0){
                helper.setCc(cc);
            }

            // 如果存在模板,定義郵件模板中的內容,context的內容對應email.html的${project}佔位的內容
            if(context != null && StringUtils.isNotBlank(templateName)){
                String emailContent = templateEngine.process(templateName, context);
                helper.setText(emailContent, true);
            }else{
                helper.setText(text);
            }

            // 如果存在附件,定義郵件的附件
            if(attachmentList != null && attachmentList.size() > 0){
                for (String attachment : attachmentList) {
                    FileSystemResource file = new FileSystemResource(attachment);
                    helper.addAttachment(file.getFilename(), file);
                }
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
  • 配置發送郵件的模板
    在resources目錄下創建templates目錄。創建email.html模板。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>yimcarson</title>
    <style>
        body {
            text-align: center;
            margin-left: auto;
            margin-right: auto;
        }
        #main {
            text-align: center;
        }
    </style>
</head>
<body>
<div id="main">
    <h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
    Your Verification Code is
    <h2><span th:text="${code}"></span></h2>
</div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章