1.使用工具:eclipse maven
2.项目结构目录
3.在pom.xml中导入依赖
<!-- 邮箱依赖jar -->
<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>
4.配置src/main/resources
建立static和templates文件夹,分别创建文件和模板info.html
info.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
这是一封测试邮件:${username}
</body>
</html>
配置application.properties
1)如果是qq邮箱,配置:
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=发件人邮箱
#QQ邮箱的授权码(QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码)
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
spring.mail.default-encoding=UTF-8
2)如果是163邮箱,配置:
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
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
spring.mail.default-encoding=UTF-8
5.书写启动类
package com.luxin.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages="com.luxin.mail")
public class SpringApplications {
public static void main(String[] args) {
SpringApplication.run(SpringApplications.class, args);
}
}
6.书写邮箱接口类
package com.luxin.mail;
import java.io.File;
public interface EmailService {
//发送简单邮件
void sendSimpleEmail(String sendTo, String title, String content);
//发送带附件的邮件
void sendAttachEmail(String sendTo, String title, String content, File file);
//使用模板发送的邮件
void sendFreeMarkerEmail(String sendTo, String title, String info);
}
7.书写邮箱实现类
package com.luxin.mail;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
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.FreeMarkerConfig;
import freemarker.template.Template;
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private SendFrom sendFrom;
@Autowired
private JavaMailSender mailSender;
@Autowired
private FreeMarkerConfig freeMarkerConfig;
//发送简单邮件
public void sendSimpleEmail(String sendTo, String title, String content) {
//简单邮件发送
SimpleMailMessage mail = new SimpleMailMessage();
mail.setSubject(title);
mail.setText(content);
mail.setFrom(sendFrom.getSendFrom());
mail.setTo(sendTo);
mailSender.send(mail);
}
//发送带附件的邮件
public void sendAttachEmail(String sendTo, String title, String content, File file) {
MimeMessage mail = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setSubject(title);
helper.setText(content);
helper.setFrom(sendFrom.getSendFrom());
helper.setTo(sendTo);
FileSystemResource fsr = new FileSystemResource(file);
helper.addAttachment("附件", fsr);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(mail);
}
//使用模板发送的邮件
public void sendFreeMarkerEmail(String sendTo, String title, String info) {
MimeMessage mail = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setSubject(title);
helper.setFrom(sendFrom.getSendFrom());
helper.setTo(sendTo);
//封装模板所使用到的数据
Map<String, Object> model = new HashMap<String, Object>();
model.put("username", "张三");
//得到模板
Template template = freeMarkerConfig.getConfiguration().getTemplate(info);
//将数据填充到模板
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
//设置成邮件正文
helper.setText(html,true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(mail);
}
}
8.书写控制层controller类:
package com.luxin.mail;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmailController {
@Autowired
private EmailServiceImpl emailServiceImpl;
@RequestMapping("simple")
public String simple(){
emailServiceImpl.sendSimpleEmail("收件人@qq.com", "测试邮件", "这是一封测试java的邮件");
return "ok";
}
@RequestMapping("attach")
public String attach(){
File file = new File("src/main/resources/static/1.png");
emailServiceImpl.sendAttachEmail("收件人@qq.com", "测试邮件", "这是一封测试java的带附件邮件",file);
return "ok";
}
@RequestMapping("templates")
public String templates(){
emailServiceImpl.sendFreeMarkerEmail("收件人@qq.com", "测试邮件", "info.html");
return "ok";
}
}