10.整合email
整合email
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
spring.mail.host=smtp.qq.com
spring.mail.username=xxx@qq.com
# 授权码 qq邮箱->设置->账户->pop3/SMTP服务
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
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;
}
}
import freemarker.template.Template;
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 javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Service
public class EmailService {
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public void sendSimpleMail(String sendTo,String subject,String content){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(emailConfig.getEmailFrom());
mailMessage.setTo(sendTo);
mailMessage.setSubject(subject);
mailMessage.setText(content);
javaMailSender.send(mailMessage);
}
//发送带附件的邮件
public void sendAttachmentMail(String sendTo, String subject, String content, File file){
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content);
helper.addAttachment("附件",new FileSystemResource(file));//文件名,文件
} catch (Exception e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
//发送模版邮件
//这里用的是freemarker
public void sendTemplateMail(String sendTo, String subject, String content, String templateName){
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(sendTo);
helper.setSubject(subject);
Map<String,Object> model = new HashMap<>();
model.put("name",content);
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html,true);
} catch (Exception e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
}
测试
import com.app.SpringDemoApp;
import com.fly.email.EmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailServiceTest {
@Autowired
private EmailService emailService;
@Test
public void test1(){
emailService.sendSimpleMail("xxx@qq.com","test","test email");
}
@Test
public void test2(){
File file = new File("src/main/resources/banner.txt");
emailService.sendAttachmentMail("xxx@qq.com","test","test email2",file);
}
@Test
public void test3(){
emailService.sendTemplateMail("xxx@qq.com","test","test email3","index.ftl");
}
}
templates/index.ftl
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index</title>
</head>
<body>
<h4 style="color: red">${name}</h4>
</body>
</html>