diff --git a/22.Spring-Boot-Email/pom.xml b/22.Spring-Boot-Email/pom.xml new file mode 100644 index 0000000..8aa1821 --- /dev/null +++ b/22.Spring-Boot-Email/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + com.springboot + Spring-Boot-Email + 0.0.1-SNAPSHOT + jar + + Spring-Boot-Email + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.7 + 3.0.2.RELEASE + 2.0.1 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-mail + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + + nexus-aliyun + Nexus aliyun + http://maven.aliyun.com/nexus/content/groups/public + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/22.Spring-Boot-Email/src/main/java/com/springboot/demo/DemoApplication.java b/22.Spring-Boot-Email/src/main/java/com/springboot/demo/DemoApplication.java new file mode 100644 index 0000000..62e53dc --- /dev/null +++ b/22.Spring-Boot-Email/src/main/java/com/springboot/demo/DemoApplication.java @@ -0,0 +1,11 @@ +package com.springboot.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/22.Spring-Boot-Email/src/main/java/com/springboot/demo/controller/EmailController.java b/22.Spring-Boot-Email/src/main/java/com/springboot/demo/controller/EmailController.java new file mode 100644 index 0000000..04fdfd6 --- /dev/null +++ b/22.Spring-Boot-Email/src/main/java/com/springboot/demo/controller/EmailController.java @@ -0,0 +1,130 @@ +package com.springboot.demo.controller; + +import java.io.File; + +import javax.mail.internet.MimeMessage; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; + +@RestController +@RequestMapping("/email") +public class EmailController { + + @Autowired + private JavaMailSender jms; + + @Value("${spring.mail.username}") + private String from; + + @Autowired + private TemplateEngine templateEngine; + + @RequestMapping("sendSimpleEmail") + public String sendSimpleEmail() { + try { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(from); + message.setTo("888888@qq.com"); // 接收地址 + message.setSubject("一封简单的邮件"); // 标题 + message.setText("使用Spring Boot发送简单邮件。"); // 内容 + jms.send(message); + return "发送成功"; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } + + @RequestMapping("sendHtmlEmail") + public String sendHtmlEmail() { + MimeMessage message = null; + try { + message = jms.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo("888888@qq.com"); // 接收地址 + helper.setSubject("一封HTML格式的邮件"); // 标题 + // 带HTML格式的内容 + StringBuffer sb = new StringBuffer("

使用Spring Boot发送HTML格式邮件。

"); + helper.setText(sb.toString(), true); + jms.send(message); + return "发送成功"; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } + + @RequestMapping("sendAttachmentsMail") + public String sendAttachmentsMail() { + MimeMessage message = null; + try { + message = jms.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo("888888@qq.com"); // 接收地址 + helper.setSubject("一封带附件的邮件"); // 标题 + helper.setText("详情参见附件内容!"); // 内容 + // 传入附件 + FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/file/项目文档.docx")); + helper.addAttachment("项目文档.docx", file); + jms.send(message); + return "发送成功"; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } + + @RequestMapping("sendInlineMail") + public String sendInlineMail() { + MimeMessage message = null; + try { + message = jms.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo("888888@qq.com"); // 接收地址 + helper.setSubject("一封带静态资源的邮件"); // 标题 + helper.setText("博客图:", true); // 内容 + // 传入附件 + FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/img/sunshine.png")); + helper.addInline("img", file); + jms.send(message); + return "发送成功"; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } + + @RequestMapping("sendTemplateEmail") + public String sendTemplateEmail(String code) { + MimeMessage message = null; + try { + message = jms.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo("888888@qq.com"); // 接收地址 + helper.setSubject("邮件摸板测试"); // 标题 + // 处理邮件模板 + Context context = new Context(); + context.setVariable("code", code); + String template = templateEngine.process("emailTemplate", context); + helper.setText(template, true); + jms.send(message); + return "发送成功"; + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } +} diff --git a/22.Spring-Boot-Email/src/main/resources/application.yml b/22.Spring-Boot-Email/src/main/resources/application.yml new file mode 100644 index 0000000..cc6e8af --- /dev/null +++ b/22.Spring-Boot-Email/src/main/resources/application.yml @@ -0,0 +1,15 @@ +server: + port: 80 + +spring: + mail: + host: smtp.163.com + username: xxxx@163.com + password: xxxx + properties: + mail: + smtp: + auth: true + starttls: + enable: true + required: true \ No newline at end of file diff --git a/22.Spring-Boot-Email/src/main/resources/static/file/项目文档.docx b/22.Spring-Boot-Email/src/main/resources/static/file/项目文档.docx new file mode 100644 index 0000000..82b681e Binary files /dev/null and b/22.Spring-Boot-Email/src/main/resources/static/file/项目文档.docx differ diff --git a/22.Spring-Boot-Email/src/main/resources/static/img/sunshine.png b/22.Spring-Boot-Email/src/main/resources/static/img/sunshine.png new file mode 100644 index 0000000..aad9d49 Binary files /dev/null and b/22.Spring-Boot-Email/src/main/resources/static/img/sunshine.png differ diff --git a/22.Spring-Boot-Email/src/main/resources/templates/emailTemplate.html b/22.Spring-Boot-Email/src/main/resources/templates/emailTemplate.html new file mode 100644 index 0000000..fae0af6 --- /dev/null +++ b/22.Spring-Boot-Email/src/main/resources/templates/emailTemplate.html @@ -0,0 +1,10 @@ + + + + + 模板 + + + 您好,您的验证码为,请在两分钟内使用完成操作。 + + \ No newline at end of file diff --git a/22.Spring-Boot-Email/src/test/java/com/springboot/demo/DemoApplicationTests.java b/22.Spring-Boot-Email/src/test/java/com/springboot/demo/DemoApplicationTests.java new file mode 100644 index 0000000..228ddb8 --- /dev/null +++ b/22.Spring-Boot-Email/src/test/java/com/springboot/demo/DemoApplicationTests.java @@ -0,0 +1,16 @@ +package com.springboot.demo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class DemoApplicationTests { + + @Test + public void contextLoads() { + } + +}