1、发送简单邮件
2、发送带附件的邮件
3、发送模板邮件
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>JpaAndTxDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>boot-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=80 server.servlet.context-path=/ #禁用freemarker缓存 spring.freemarker.cache=false logging.level.root=info logging.file=d:/logs/boot-demo.log #发送邮件的配置 spring.mail.host=smtp.163.com spring.mail.username=xxx@163.com spring.mail.password=yyy spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
EmailConfig
package com.oy.mail; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class EmailConfig { @Value("${spring.mail.username}") private String emailFrom; public String getEmailFrom() { return emailFrom; } public void setEmailFrom(String emailFrom) { this.emailFrom = emailFrom; } }
EmailController
package com.oy.mail; import java.io.File; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class EmailController { @Autowired private EmailService emailService; @RequestMapping("/simple") @ResponseBody public String sendSimpleEmail() { emailService.sendSimpleMail("收件人", "测试标题", "测试内容"); return "ok"; } @RequestMapping("/attach") @ResponseBody public String sendAttachmentEmail() { File file = new File("src/main/resources/static/1.txt"); emailService.sendAttachmentMail("收件人", "测试标题", "测试内容", file); return "ok"; } @RequestMapping("/temp") @ResponseBody public String sendTemplateEmail() { emailService.sendTemplateMail("收件人", "测试标题", "test.html"); return "ok"; } }
EmailService
package com.oy.mail; import java.io.File; public interface EmailService { // 发送简单邮件 void sendSimpleMail(String sendTo, String title, String content); // 发送带附件的邮件 void sendAttachmentMail(String sendTo, String title, String content, File file); // 发送模板邮件; 第三个参数:模板文件名 void sendTemplateMail(String sendTo, String title, String templateFileName); }
EmailServiceImpl
package com.oy.mail; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import freemarker.template.Template; @Service public class EmailServiceImpl implements EmailService { @Autowired private EmailConfig emailConfig; @Autowired private JavaMailSender mailSender; @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; // 发送简单邮件 @Override public void sendSimpleMail(String sendTo, String title, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(emailConfig.getEmailFrom()); message.setTo(sendTo); message.setSubject(title); message.setText(content); mailSender.send(message); } // 发送带附件的邮件 @Override public void sendAttachmentMail(String sendTo, String title, String content, File file) { MimeMessage msg = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setFrom(emailConfig.getEmailFrom()); helper.setTo(sendTo); helper.setSubject(title); helper.setText(content); // 附件 FileSystemResource r = new FileSystemResource(file); helper.addAttachment(file.getName(), r); // 第一个参数为发送的附件显示的名称 } catch (Exception e) { e.printStackTrace(); } mailSender.send(msg); } // 发送模板邮件 @Override public void sendTemplateMail(String sendTo, String title, String templateFileName) { MimeMessage msg = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setFrom(emailConfig.getEmailFrom()); helper.setTo(sendTo); helper.setSubject(title); // 封装模板使用的数据 Map<String, Object> model = new HashMap<>(); model.put("msg", "邮件内容文本"); // 得到模板 Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateFileName); String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); System.out.println("模板内容:" + html); helper.setText(html, true); } catch (Exception e) { e.printStackTrace(); } mailSender.send(msg); } }
发送模板邮件的测试模板test.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <font color="red">${msg}</font> </body> </html>
---