Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
74a5a03a
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
74a5a03a
编写于
12月 19, 2022
作者:
Q
qinyingjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:发邮件
上级
c3a6f85c
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
265 addition
and
1 deletion
+265
-1
pom.xml
pom.xml
+4
-0
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
+28
-0
src/main/java/com/kwan/springbootkwan/service/ISendMsgHandle.java
.../java/com/kwan/springbootkwan/service/ISendMsgHandle.java
+28
-0
src/main/java/com/kwan/springbootkwan/service/impl/EmailSendMsgHandleImpl.java
...n/springbootkwan/service/impl/EmailSendMsgHandleImpl.java
+176
-0
src/main/resources/application.yaml
src/main/resources/application.yaml
+13
-0
src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
...est/java/com/kwan/springbootkwan/UserServiceImplTest.java
+16
-1
未找到文件。
pom.xml
浏览文件 @
74a5a03a
...
@@ -109,6 +109,10 @@
...
@@ -109,6 +109,10 @@
<artifactId>
springfox-swagger-ui
</artifactId>
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
2.6.1
</version>
<version>
2.6.1
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-mail
</artifactId>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
<plugins>
<plugins>
...
...
src/main/java/com/kwan/springbootkwan/entity/MailInfo.java
0 → 100644
浏览文件 @
74a5a03a
package
com.kwan.springbootkwan.entity
;
import
lombok.Data
;
import
java.util.Map
;
/**
* 邮件实体
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 22:41
*/
@Data
public
class
MailInfo
{
// 邮件接收人
private
String
[]
receiver
;
// 邮件主题
private
String
subject
;
// 邮件的文本内容
private
String
content
;
// 抄送人
private
String
[]
cc
;
// 邮件附件的文件名
private
String
[]
attachFileNames
;
// 邮件内容内嵌图片
private
Map
<
String
,
String
>
imageMap
;
}
src/main/java/com/kwan/springbootkwan/service/ISendMsgHandle.java
0 → 100644
浏览文件 @
74a5a03a
package
com.kwan.springbootkwan.service
;
import
com.kwan.springbootkwan.entity.MailInfo
;
public
interface
ISendMsgHandle
{
/**
* 功能描述:发送text邮件
*
* @param mailInfo 邮件信息
*/
void
sendSimpleTextEmail
(
MailInfo
mailInfo
);
/**
* 功能描述:发送html邮件
*
* @param mailInfo 邮件信息
* @param html 是否是html邮件
*/
void
sendHtmlEmail
(
MailInfo
mailInfo
,
boolean
html
);
/**
* 功能描述:发送文本内容(内嵌图片)且带附件的html邮件
*
* @param mailInfo 邮件信息
*/
void
sendEnclosureEmail
(
MailInfo
mailInfo
);
}
src/main/java/com/kwan/springbootkwan/service/impl/EmailSendMsgHandleImpl.java
0 → 100644
浏览文件 @
74a5a03a
package
com.kwan.springbootkwan.service.impl
;
import
com.kwan.springbootkwan.entity.MailInfo
;
import
com.kwan.springbootkwan.service.ISendMsgHandle
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.mail.SimpleMailMessage
;
import
org.springframework.mail.javamail.JavaMailSender
;
import
org.springframework.mail.javamail.MimeMessageHelper
;
import
org.springframework.stereotype.Component
;
import
javax.activation.DataHandler
;
import
javax.activation.FileDataSource
;
import
javax.annotation.Resource
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMultipart
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
@Component
public
class
EmailSendMsgHandleImpl
implements
ISendMsgHandle
{
@Resource
private
JavaMailSender
javaMailSender
;
@Value
(
value
=
"${spring.mail.username}"
)
private
String
emailFrom
;
@Override
public
void
sendSimpleTextEmail
(
MailInfo
mailInfo
)
{
try
{
SimpleMailMessage
mailMessage
=
new
SimpleMailMessage
();
//发件人
mailMessage
.
setFrom
(
emailFrom
);
//接收人
mailMessage
.
setTo
(
mailInfo
.
getReceiver
());
//邮件主题
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
//邮件抄送
mailMessage
.
setCc
(
mailInfo
.
getCc
());
//邮件内容
mailMessage
.
setText
(
mailInfo
.
getContent
());
//发送邮件
javaMailSender
.
send
(
mailMessage
);
}
catch
(
Exception
e
)
{
log
.
error
(
"邮件发送失败:{}"
,
e
.
getMessage
());
}
}
@Override
public
void
sendHtmlEmail
(
MailInfo
mailInfo
,
boolean
html
)
{
try
{
MimeMessage
mimeMailMessage
=
javaMailSender
.
createMimeMessage
();
//true 表示需要创建一个multipart message
MimeMessageHelper
mimeMessageHelper
=
new
MimeMessageHelper
(
mimeMailMessage
);
//发件人
mimeMessageHelper
.
setFrom
(
emailFrom
);
//接收人
mimeMessageHelper
.
setTo
(
mailInfo
.
getReceiver
());
//邮件主题
mimeMessageHelper
.
setSubject
(
mailInfo
.
getSubject
());
//邮件抄送
mimeMessageHelper
.
setCc
(
mailInfo
.
getCc
());
//邮件内容
mimeMessageHelper
.
setText
(
mailInfo
.
getContent
(),
html
);
javaMailSender
.
send
(
mimeMailMessage
);
}
catch
(
Exception
e
)
{
log
.
error
(
"邮件发送失败:{}"
,
e
.
getMessage
());
}
}
@Override
public
void
sendEnclosureEmail
(
MailInfo
mailInfo
)
{
try
{
MimeMessage
mimeMailMessage
=
javaMailSender
.
createMimeMessage
();
//true 表示需要创建一个multipart message
MimeMessageHelper
mimeMessageHelper
=
new
MimeMessageHelper
(
mimeMailMessage
,
true
);
//发件人
mimeMessageHelper
.
setFrom
(
emailFrom
);
//邮件接收人
mimeMessageHelper
.
setTo
(
mailInfo
.
getReceiver
());
//邮件主题
mimeMessageHelper
.
setSubject
(
mailInfo
.
getSubject
());
//邮件抄送
mimeMessageHelper
.
setCc
(
mailInfo
.
getCc
());
//设置显示的发件时间
mimeMessageHelper
.
setSentDate
(
new
Date
());
MimeMultipart
allMultipart
=
new
MimeMultipart
();
//创建代表邮件正文和附件的各个MimeBodyPart对象
MimeBodyPart
contentPart
=
createContent
(
mailInfo
.
getContent
(),
mailInfo
.
getImageMap
());
allMultipart
.
addBodyPart
(
contentPart
);
//创建用于组合邮件正文和附件的MimeMultipart对象
for
(
int
i
=
0
;
i
<
mailInfo
.
getAttachFileNames
().
length
;
i
++)
{
allMultipart
.
addBodyPart
(
createAttachment
(
mailInfo
.
getAttachFileNames
()[
i
]));
}
//设置整个邮件内容为最终组合出的MimeMultipart对象
mimeMailMessage
.
setContent
(
allMultipart
);
mimeMailMessage
.
saveChanges
();
javaMailSender
.
send
(
mimeMailMessage
);
}
catch
(
Exception
e
)
{
log
.
error
(
"邮件发送失败:{}"
,
e
.
getMessage
());
}
}
/**
* 功能描述:邮件附件
*
* @param filename 文件路径
* @return {@link MimeBodyPart}
* @author wi-gang
* @date 2021/12/29 2:50 下午
*/
public
MimeBodyPart
createAttachment
(
String
filename
)
throws
Exception
{
//创建保存附件的MimeBodyPart对象,并加入附件内容和相应的信息
MimeBodyPart
attachPart
=
new
MimeBodyPart
();
FileDataSource
fsd
=
new
FileDataSource
(
filename
);
attachPart
.
setDataHandler
(
new
DataHandler
(
fsd
));
attachPart
.
setFileName
(
fsd
.
getName
());
return
attachPart
;
}
/**
* 功能描述:文本内容
*
* @param body html格式的文本内容
* @return {@link MimeBodyPart}
* @author wi-gang
* @date 2021/12/29 2:50 下午
*/
public
MimeBodyPart
createContent
(
String
body
)
throws
Exception
{
//创建代表组合Mime消息的MimeMultipart对象,将该MimeMultipart对象保存到MimeBodyPart对象
MimeBodyPart
contentPart
=
new
MimeBodyPart
();
MimeMultipart
contentMultipart
=
new
MimeMultipart
(
"related"
);
//创建用于保存HTML正文的MimeBodyPart对象,并将它保存到MimeMultipart中
MimeBodyPart
htmlBodyPart
=
new
MimeBodyPart
();
htmlBodyPart
.
setContent
(
body
,
"text/html;charset=UTF-8"
);
contentMultipart
.
addBodyPart
(
htmlBodyPart
);
//将MimeMultipart对象保存到MimeBodyPart对象
contentPart
.
setContent
(
contentMultipart
);
return
contentPart
;
}
/**
* 功能描述:文本内嵌图片
*
* @param body html格式的文本内容
* @param map 图片集合
* @return {@link MimeBodyPart}
* @author wi-gang
* @date 2021/12/29 2:50 下午
*/
public
MimeBodyPart
createContent
(
String
body
,
Map
<
String
,
String
>
map
)
throws
Exception
{
//创建代表组合Mime消息的MimeMultipart对象,将该MimeMultipart对象保存到MimeBodyPart对象
MimeBodyPart
contentPart
=
new
MimeBodyPart
();
MimeMultipart
contentMultipart
=
new
MimeMultipart
(
"related"
);
//创建用于保存HTML正文的MimeBodyPart对象,并将它保存到MimeMultipart中
MimeBodyPart
htmlBodyPart
=
new
MimeBodyPart
();
htmlBodyPart
.
setContent
(
body
,
"text/html;charset=UTF-8"
);
contentMultipart
.
addBodyPart
(
htmlBodyPart
);
if
(
map
!=
null
&&
map
.
size
()
>
0
)
{
Set
<
Map
.
Entry
<
String
,
String
>>
set
=
map
.
entrySet
();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
set
)
{
//创建用于保存图片的MimeBodyPart对象,并将它保存到MimeMultipart中
MimeBodyPart
gifBodyPart
=
new
MimeBodyPart
();
FileDataSource
fds
=
new
FileDataSource
(
entry
.
getValue
());
//图片所在的目录的绝对路径
gifBodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
gifBodyPart
.
setContentID
(
entry
.
getKey
());
//cid的值
contentMultipart
.
addBodyPart
(
gifBodyPart
);
}
}
//将MimeMultipart对象保存到MimeBodyPart对象
contentPart
.
setContent
(
contentMultipart
);
return
contentPart
;
}
}
\ No newline at end of file
src/main/resources/application.yaml
浏览文件 @
74a5a03a
...
@@ -6,6 +6,19 @@ swagger:
...
@@ -6,6 +6,19 @@ swagger:
#兼容swagger配置
#兼容swagger配置
spring
:
spring
:
mail
:
host
:
smtp.qq.com
# 发送者的邮箱账号
username
:
327782001@qq.com
# 邮箱密码授权码
password
:
kypxnmwfniqrcbeh
properties
:
mail
:
smtp
:
auth
:
true
starttls
:
enable
:
true
required
:
true
redis
:
redis
:
database
:
0
# Redis数据库索引(默认为0)
database
:
0
# Redis数据库索引(默认为0)
host
:
120.79.36.53
#Redis服务器地址
host
:
120.79.36.53
#Redis服务器地址
...
...
src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
浏览文件 @
74a5a03a
package
com.kwan.springbootkwan
;
package
com.kwan.springbootkwan
;
import
com.kwan.springbootkwan.entity.MailInfo
;
import
com.kwan.springbootkwan.entity.User
;
import
com.kwan.springbootkwan.entity.User
;
import
com.kwan.springbootkwan.mapper.UserMapper
;
import
com.kwan.springbootkwan.mapper.UserMapper
;
import
com.kwan.springbootkwan.service.ISendMsgHandle
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
@SpringBootTest
@SpringBootTest
public
class
UserServiceImplTest
{
public
class
UserServiceImplTest
{
@Autowired
@Autowired
private
UserMapper
userService
;
private
UserMapper
userService
;
@Autowired
private
ISendMsgHandle
isSendMsgHandle
;
@Test
@Test
public
void
queryAll
()
{
public
void
queryAll
()
{
User
user
=
userService
.
selectById
(
1
);
User
user
=
userService
.
selectById
(
1
);
System
.
out
.
println
(
user
);
System
.
out
.
println
(
user
);
}
}
/**
* 发邮件
*/
@Test
void
sendSimpleTextEmail
()
{
MailInfo
mailInfo
=
new
MailInfo
();
mailInfo
.
setReceiver
(
new
String
[]{
"qinyingjie@deepexi.com"
});
mailInfo
.
setSubject
(
"测试主题"
);
mailInfo
.
setContent
(
"邮件内容"
);
isSendMsgHandle
.
sendSimpleTextEmail
(
mailInfo
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录