Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
bluewinestar
spring-boot-examples
提交
853b7dfb
S
spring-boot-examples
项目概览
bluewinestar
/
spring-boot-examples
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-boot-examples
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
853b7dfb
编写于
5月 04, 2017
作者:
微笑很纯洁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add mail service
上级
d4588da0
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
343 addition
and
0 deletion
+343
-0
README.md
README.md
+3
-0
spring-boot-mail/pom.xml
spring-boot-mail/pom.xml
+71
-0
spring-boot-mail/src/main/java/com/neo/Application.java
spring-boot-mail/src/main/java/com/neo/Application.java
+13
-0
spring-boot-mail/src/main/java/com/neo/service/MailService.java
...-boot-mail/src/main/java/com/neo/service/MailService.java
+16
-0
spring-boot-mail/src/main/java/com/neo/service/impl/MailServiceImpl.java
...l/src/main/java/com/neo/service/impl/MailServiceImpl.java
+138
-0
spring-boot-mail/src/main/resources/application.properties
spring-boot-mail/src/main/resources/application.properties
+9
-0
spring-boot-mail/src/main/resources/templates/emailTemplate.html
...boot-mail/src/main/resources/templates/emailTemplate.html
+11
-0
spring-boot-mail/src/test/java/com/neo/ApplicationTests.java
spring-boot-mail/src/test/java/com/neo/ApplicationTests.java
+17
-0
spring-boot-mail/src/test/java/com/neo/service/MailServiceTest.java
...t-mail/src/test/java/com/neo/service/MailServiceTest.java
+65
-0
未找到文件。
README.md
浏览文件 @
853b7dfb
...
...
@@ -12,6 +12,7 @@ Spring boot使用的各种示例,以最简单、最实用为标准
-
[
spring-boot-rabbitmq
](
https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-rabbitmq
)
:spring boot和rabbitmq各种消息应用案例
-
[
spring-boot-scheduler
](
https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-scheduler
)
:spring boot和定时任务案例
-
[
spring-boot-web
](
https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-web
)
:web开发综合使用案例
-
[
spring-boot-mail
](
https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-mail
)
:spring boot和邮件服务
-
[
Favorites-web
](
https://github.com/cloudfavorites/favorites-web
)
:云收藏(springboot实战开源软件)
...
...
@@ -27,4 +28,6 @@ Spring boot使用的各种示例,以最简单、最实用为标准
-
[
springboot(七):springboot+mybatis多数据源最简解决方案
](
http://www.ityouknow.com/springboot/2016/11/25/springboot(%E4%B8%83
)
-springboot+mybatis%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%9C%80%E7%AE%80%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88.html)
-
[
springboot(八):RabbitMQ详解
](
http://www.ityouknow.com/springboot/2016/11/30/springboot(%E5%85%AB
)
-RabbitMQ%E8%AF%A6%E8%A7%A3.html)
-
[
springboot(九):定时任务
](
http://www.ityouknow.com/springboot/2016/12/02/springboot(%E4%B9%9D
)
-%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1.html)
-
[
springboot(十):邮件服务
](
http://www.ityouknow.com/life/2017/05/06/springboot-mail.html
)
-
[
springboot实战:我们的第一款开源软件
](
http://www.ityouknow.com/springboot/2016/09/26/springboot%E5%AE%9E%E6%88%98-%E6%88%91%E4%BB%AC%E7%9A%84%E7%AC%AC%E4%B8%80%E6%AC%BE%E5%BC%80%E6%BA%90%E8%BD%AF%E4%BB%B6.html
)
spring-boot-mail/pom.xml
0 → 100644
浏览文件 @
853b7dfb
<?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.3.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-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context-support
</artifactId>
<version>
RELEASE
</version>
</dependency>
<dependency>
<groupId>
com.sun.mail
</groupId>
<artifactId>
javax.mail
</artifactId>
<version>
RELEASE
</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-thymeleaf
</artifactId>
</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>
spring-boot-mail/src/main/java/com/neo/Application.java
0 → 100644
浏览文件 @
853b7dfb
package
com.neo
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.scheduling.annotation.EnableScheduling
;
@SpringBootApplication
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
spring-boot-mail/src/main/java/com/neo/service/MailService.java
0 → 100644
浏览文件 @
853b7dfb
package
com.neo.service
;
/**
* Created by summer on 2017/5/4.
*/
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
);
}
spring-boot-mail/src/main/java/com/neo/service/impl/MailServiceImpl.java
0 → 100644
浏览文件 @
853b7dfb
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
;
/**
* Created by summer on 2017/5/4.
*/
@Component
public
class
MailServiceImpl
implements
MailService
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
@Autowired
private
JavaMailSender
mailSender
;
@Value
(
"${mail.fromMail.addr}"
)
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
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
File
.
separator
));
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
);
}
}
}
spring-boot-mail/src/main/resources/application.properties
0 → 100644
浏览文件 @
853b7dfb
spring.application.name
=
spirng-boot-mail
spring.mail.host
=
smtp.163.com
spring.mail.username
=
xxoo@xxoo.com
spring.mail.password
=
xxoo
spring.mail.default-encoding
=
UTF-8
mail.fromMail.addr
=
xxoo@xxoo.com
spring-boot-mail/src/main/resources/templates/emailTemplate.html
0 → 100644
浏览文件 @
853b7dfb
<!DOCTYPE html>
<html
lang=
"zh"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
/>
<title>
Title
</title>
</head>
<body>
您好,这是验证邮件,请点击下面的链接完成验证,
<br/>
<a
href=
"#"
th:href=
"@{ http://www.ityouknow.com/neo/{id}(id=${id}) }"
>
激活账号
</a>
</body>
</html>
\ No newline at end of file
spring-boot-mail/src/test/java/com/neo/ApplicationTests.java
0 → 100644
浏览文件 @
853b7dfb
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
ApplicationTests
{
@Test
public
void
contextLoads
()
{
System
.
out
.
println
(
"hello world"
);
}
}
spring-boot-mail/src/test/java/com/neo/service/MailServiceTest.java
0 → 100644
浏览文件 @
853b7dfb
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
;
/**
* Created by summer on 2017/5/4.
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
MailServiceTest
{
@Autowired
private
MailService
mailService
;
@Autowired
private
TemplateEngine
templateEngine
;
@Test
public
void
testSimpleMail
()
throws
Exception
{
mailService
.
sendSimpleMail
(
"ityouknow@126.com"
,
"test simple mail"
,
" hello this is simple mail"
);
}
@Test
public
void
testHtmlMail
()
throws
Exception
{
String
content
=
"<html>\n"
+
"<body>\n"
+
" <h3>hello world ! 这是一封html邮件!</h3>\n"
+
"</body>\n"
+
"</html>"
;
mailService
.
sendHtmlMail
(
"ityouknow@126.com"
,
"test simple mail"
,
content
);
}
@Test
public
void
sendAttachmentsMail
()
{
String
filePath
=
"e:\\tmp\\application.log"
;
mailService
.
sendAttachmentsMail
(
"ityouknow@126.com"
,
"主题:带附件的邮件"
,
"有附件,请查收!"
,
filePath
);
}
@Test
public
void
sendInlineResourceMail
()
{
String
rscId
=
"neo006"
;
String
content
=
"<html><body>这是有图片的邮件:<img src=\'cid:"
+
rscId
+
"\' ></body></html>"
;
String
imgPath
=
"C:\\Users\\summer\\Pictures\\favicon.png"
;
mailService
.
sendInlineResourceMail
(
"ityouknow@126.com"
,
"主题:这是有图片的邮件"
,
content
,
imgPath
,
rscId
);
}
@Test
public
void
sendTemplateMail
()
{
//创建邮件正文
Context
context
=
new
Context
();
context
.
setVariable
(
"id"
,
"006"
);
String
emailContent
=
templateEngine
.
process
(
"emailTemplate"
,
context
);
mailService
.
sendHtmlMail
(
"ityouknow@126.com"
,
"主题:这是模板邮件"
,
emailContent
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录