Skip to content

Commit

Permalink
add Mail Quartz
Browse files Browse the repository at this point in the history
  • Loading branch information
ityouknow committed Nov 25, 2017
1 parent eb2bb38 commit b7134c3
Show file tree
Hide file tree
Showing 26 changed files with 727 additions and 2 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# gitChat 达人课 示例代码

# Spring Boot精选课程

[课程地址](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba)


达人课内容:

- [第01课:课程概要](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f5e21449cd433061360883)
- [第02课:快速实战 Spring Boot](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f68c4f49cd43306136301c)
- [第03课:快速体验 Web 开发](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f6922549cd4330613634a4)
- [第04课:Spring Data JPA 的使用](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f6a809a5beea6a3fd8a7f2)
- [第05课:模板引擎 Thymeleaf](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f6d537a5beea6a3fd8c216)
- [第06课:JPA 和 Thymeleaf 实践](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f6f2dba5beea6a3fd8d5b0)
- [第07课:Spring Boot 集成 MyBatis](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f97e7e68673133615f7427)
- [第08课:MyBatis Druid 多数据源](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f97ed968673133615f745f)
- [第08课:MyBatis Druid 多数据源](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f97f0d68673133615f7481)
- [第09课:如何玩转 Redis](http://www.ityouknow.com/springboot/2017/05/06/springboot-mail.html)
- [第10课:Redis 实现数据缓存和 Session 共享](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f97f3f68673133615f749b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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>

<groupId>com.neo</groupId>
<artifactId>spring-boot-mail</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>spring-boot-mail</name>
<description>Demo project for Spring Boot and mail</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.neo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MailApplication {

public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.neo.service;


public interface MailService {

public void sendSimpleMail(String to, String subject, String content);

public void sendHtmlMail(String to, String subject, String content);

public void sendAttachmentsMail(String to, String subject, String content, String filePath);

public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.neo.service.impl;

import com.neo.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


@Component
public class MailServiceImpl implements MailService{

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private JavaMailSender mailSender;

@Value("${spring.mail.username}")
private String from;

/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);

try {
mailSender.send(message);
logger.info("简单邮件已经发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}

}

/**
* 发送html邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();

try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);

mailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}


/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath){
MimeMessage message = mailSender.createMimeMessage();

try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);

FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
//helper.addAttachment("test"+fileName, file);

mailSender.send(message);
logger.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}


/**
* 发送正文中有静态资源(图片)的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){
MimeMessage message = mailSender.createMimeMessage();

try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);

FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);

mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.application.name=spirng-boot-mail

spring.mail.host=smtp.126.com
spring.mail.username[email protected]
spring.mail.password=yourPassword
spring.mail.default-encoding=UTF-8

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>邮件模板</title>
</head>
<body>
您好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!<br/>
<a href="#" th:href="@{http://www.ityouknow.com/register/{id}(id=${id}) }">激活账号</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.neo;

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 MailApplicationTests {

@Test
public void contextLoads() {
System.out.println("hello world");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.neo.service;

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.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {

@Autowired
private MailService mailService;

@Autowired
private TemplateEngine templateEngine;

@Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("[email protected]","这是一封简单邮件","大家好,这是我的第一封邮件!");
}

@Test
public void testHtmlMail() throws Exception {
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("[email protected]","这是一封HTML邮件",content);
}

@Test
public void sendAttachmentsMail() {
String filePath="e:\\temp\\fastdfs-client-java-5.0.0.jar";
mailService.sendAttachmentsMail("[email protected]", "主题:带附件的邮件", "有附件,请查收!", filePath);
}


@Test
public void sendInlineResourceMail() {
String rscId = "neo006";
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "e:\\temp\\weixin.jpg";

mailService.sendInlineResourceMail("[email protected]", "主题:这是有图片的邮件", content, imgPath, rscId);
}


@Test
public void sendTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);

mailService.sendHtmlMail("[email protected]","主题:这是模板邮件",emailContent);
}
}
Loading

0 comments on commit b7134c3

Please sign in to comment.