Skip to content

Commit 6fdcfde

Browse files
committed
发送邮件-使用模板邮件并实现多账号轮询发送
1 parent 82567cf commit 6fdcfde

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1732
-0
lines changed

spring-boot-demo-24-1/pom.xml

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.roncoo.education</groupId>
7+
<artifactId>spring-boot-demo-24-1</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-demo-24-1</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.4.0.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-devtools</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-freemarker</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.webjars</groupId>
38+
<artifactId>jquery</artifactId>
39+
<version>2.1.4</version>
40+
</dependency>
41+
42+
<!-- 数据库 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-data-jpa</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.h2database</groupId>
49+
<artifactId>h2</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-data-redis</artifactId>
56+
</dependency>
57+
58+
<!-- mongodb -->
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>de.flapdoodle.embed</groupId>
65+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
66+
</dependency>
67+
68+
<!-- caching -->
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-cache</artifactId>
72+
</dependency>
73+
<dependency>
74+
<groupId>net.sf.ehcache</groupId>
75+
<artifactId>ehcache</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.apache.httpcomponents</groupId>
80+
<artifactId>httpclient</artifactId>
81+
</dependency>
82+
83+
<!-- mail -->
84+
<dependency>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-starter-mail</artifactId>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>org.springframework.boot</groupId>
91+
<artifactId>spring-boot-starter-test</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
96+
<build>
97+
<plugins>
98+
<plugin>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-maven-plugin</artifactId>
101+
</plugin>
102+
</plugins>
103+
</build>
104+
105+
106+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.roncoo.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
8+
@EnableCaching
9+
@ServletComponentScan
10+
@SpringBootApplication
11+
public class SpringBootDemo241Application {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(SpringBootDemo241Application.class, args);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
public class RoncooUser {
6+
private int id;
7+
private String name;
8+
private Date createTime;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public Date getCreateTime() {
27+
return createTime;
28+
}
29+
30+
public void setCreateTime(Date createTime) {
31+
this.createTime = createTime;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class RoncooUserLog {
12+
13+
@Id
14+
@GeneratedValue
15+
private Integer id;
16+
17+
@Column
18+
private Date createTime;
19+
20+
@Column
21+
private String userName;
22+
23+
@Column
24+
private String userIp;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public Date getCreateTime() {
35+
return createTime;
36+
}
37+
38+
public void setCreateTime(Date createTime) {
39+
this.createTime = createTime;
40+
}
41+
42+
public String getUserName() {
43+
return userName;
44+
}
45+
46+
public void setUserName(String userName) {
47+
this.userName = userName;
48+
}
49+
50+
public String getUserIp() {
51+
return userIp;
52+
}
53+
54+
public void setUserIp(String userIp) {
55+
this.userIp = userIp;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "RoncooUserLog [id=" + id + ", createTime=" + createTime + ", userName=" + userName + ", userIp=" + userIp + "]";
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.roncoo.example.cache;
2+
3+
import com.roncoo.example.bean.RoncooUserLog;
4+
5+
/**
6+
* @author wujing
7+
*/
8+
public interface RoncooUserLogCache {
9+
10+
/**
11+
* 查询
12+
*
13+
* @param id
14+
* @return
15+
*/
16+
RoncooUserLog selectById(Integer id);
17+
18+
/**
19+
* 更新
20+
*
21+
* @param roncooUserLog
22+
* @return
23+
*/
24+
RoncooUserLog updateById(RoncooUserLog roncooUserLog);
25+
26+
/**
27+
* 删除
28+
*
29+
* @param id
30+
* @return
31+
*/
32+
String deleteById(Integer id);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.roncoo.example.cache.impl;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.annotation.CacheConfig;
5+
import org.springframework.cache.annotation.CacheEvict;
6+
import org.springframework.cache.annotation.CachePut;
7+
import org.springframework.cache.annotation.Cacheable;
8+
import org.springframework.stereotype.Repository;
9+
10+
import com.roncoo.example.bean.RoncooUserLog;
11+
import com.roncoo.example.cache.RoncooUserLogCache;
12+
import com.roncoo.example.dao.RoncooUserLogDao;
13+
14+
/**
15+
* @author wujing
16+
*/
17+
@CacheConfig(cacheNames = "roncooCache")
18+
@Repository
19+
public class RoncooUserLogCacheImpl implements RoncooUserLogCache {
20+
21+
@Autowired
22+
private RoncooUserLogDao roncooUserLogDao;
23+
24+
@Cacheable(key = "#p0")
25+
@Override
26+
public RoncooUserLog selectById(Integer id) {
27+
System.out.println("查询功能,缓存找不到,直接读库, id=" + id);
28+
return roncooUserLogDao.findOne(id);
29+
}
30+
31+
@CachePut(key = "#p0")
32+
@Override
33+
public RoncooUserLog updateById(RoncooUserLog roncooUserLog) {
34+
System.out.println("更新功能,更新缓存,直接写库, id=" + roncooUserLog);
35+
return roncooUserLogDao.save(roncooUserLog);
36+
}
37+
38+
@CacheEvict(key = "#p0")
39+
@Override
40+
public String deleteById(Integer id) {
41+
System.out.println("删除功能,删除缓存,直接写库, id=" + id);
42+
return "清空缓存成功";
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.roncoo.example.component;
2+
3+
import java.io.IOException;
4+
import java.io.UnsupportedEncodingException;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import javax.mail.MessagingException;
9+
import javax.mail.internet.InternetAddress;
10+
import javax.mail.internet.MimeMessage;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.mail.javamail.MimeMessageHelper;
14+
import org.springframework.stereotype.Component;
15+
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
16+
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
17+
18+
import com.roncoo.example.util.configuration.RoncooJavaMailSenderImpl;
19+
20+
import freemarker.core.ParseException;
21+
import freemarker.template.MalformedTemplateNameException;
22+
import freemarker.template.TemplateException;
23+
import freemarker.template.TemplateNotFoundException;
24+
25+
/**
26+
*
27+
* @author wujing
28+
*/
29+
@Component
30+
public class RoncooJavaMailComponent {
31+
private static final String template = "mail/roncoo.ftl";
32+
33+
@Autowired
34+
private FreeMarkerConfigurer freeMarkerConfigurer;
35+
@Autowired
36+
private RoncooJavaMailSenderImpl javaMailSender;
37+
38+
public void sendMail(String email) {
39+
Map<String, Object> map = new HashMap<String, Object>();
40+
map.put("email", email);
41+
try {
42+
String text = getTextByTemplate(template, map);
43+
send(email, text);
44+
} catch (IOException | TemplateException e) {
45+
e.printStackTrace();
46+
} catch (MessagingException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
private String getTextByTemplate(String template, Map<String, Object> model) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
52+
return FreeMarkerTemplateUtils.processTemplateIntoString(freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
53+
}
54+
55+
private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
56+
MimeMessage message = javaMailSender.createMimeMessage();
57+
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
58+
InternetAddress from = new InternetAddress();
59+
from.setAddress(javaMailSender.getUsername());
60+
from.setPersonal("龙果学院", "UTF-8");
61+
helper.setFrom(from);
62+
helper.setTo(email);
63+
helper.setSubject("测试邮件");
64+
helper.setText(text, true);
65+
javaMailSender.send(message);
66+
return text;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)