boot发送页面的资料不少,不过以 指定页面 作为邮件内容发送的不太多,本文做笔记。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>boot</finalName>
</build>
项目结构
Controller
package com.cn.controller;
import com.cn.util.MailUtils;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
public class MailController {
@Autowired
private MailUtils mailUtils;
@RequestMapping("mail")
public void sendMail() throws MessagingException, IOException, TemplateException {
System.out.println("ssssssssssss");
mailUtils.sendMail("10524xxx@qq.com","xuxxx@163.com","mail test");
}
//跳转ftl页面
@RequestMapping("skip")
public String skip(Map<String,String> map) {
map.put("name","zhangsan");
return "mail";
}
}
Utils
package com.cn.util;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@Component
public class MailUtils {
@Autowired
private Configuration configuration;
@Autowired
private JavaMailSender javaMailSender;
public void sendMail(String from,String to,String subject) throws MessagingException, IOException, TemplateException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = null;
helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
StringWriter out = new StringWriter();
Template template = configuration.getTemplate("mail.ftl");
Map<String,String> map = new HashMap<String, String>();
//给freemarker 封装页面参数
map.put("name","zhangsan");
template.process(map,out);
helper.setText(out.toString(),true);
javaMailSender.send(mimeMessage);
}
}
application.yml
spring:
mail:
default-encoding: UTF-8
protocol: smtp
host: smtp.qq.com
port: 465
username: 1052xxxxx@qq.com
password: xxxxxxx
#指定freemarker模板位置
freemarker:
template:
ftl:
short:
path: classpath:/templates
mail.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>姓名</td>
</tr>
<tr>
<td>${name}</td>
</tr>
</table>
</body>
</html>
开启qq邮件 设置–>账户–>选项卡中找到开启POP3/SMTP选项,点击开启。
http://127.0.0.1:8080/mail 就可以进行邮件发送