init
This commit is contained in:
27
common/common-email/pom.xml
Normal file
27
common/common-email/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>common</artifactId>
|
||||
<groupId>cn.fateverse</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-email</artifactId>
|
||||
|
||||
<properties>
|
||||
<mail.vaersion>1.6.2</mail.vaersion>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${mail.vaersion}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.fateverse.common.email.config;
|
||||
|
||||
import cn.fateverse.common.email.service.EmailService;
|
||||
import cn.fateverse.common.email.service.impl.EmailServiceImpl;
|
||||
import cn.fateverse.common.email.service.session.EmailSessionProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 邮件配置类
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
@EnableConfigurationProperties({EmailProperties.class})
|
||||
public class EmailConfiguration {
|
||||
|
||||
@Bean
|
||||
EmailService emailService() {
|
||||
return new EmailServiceImpl();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(EmailSessionProvider.class)
|
||||
EmailSessionProvider emailSessionProvider() {
|
||||
return new EmailSessionProvider();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package cn.fateverse.common.email.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 系统邮件发送服务器
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "email")
|
||||
public class EmailProperties {
|
||||
/**
|
||||
* 邮件服务器发送者邮箱
|
||||
*/
|
||||
private String sender;
|
||||
/**
|
||||
* 发送人名称
|
||||
*/
|
||||
private String personal;
|
||||
/**
|
||||
* 邮件服务器
|
||||
*/
|
||||
private String emailSmtpHost;
|
||||
/**
|
||||
* 邮件服务器端口
|
||||
*/
|
||||
private String emailSmtpPort;
|
||||
/**
|
||||
* 邮件发送者用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 发送者密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 邮件服务器协议
|
||||
*/
|
||||
private String encryption;
|
||||
|
||||
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public String getPersonal() {
|
||||
return personal;
|
||||
}
|
||||
|
||||
public void setPersonal(String personal) {
|
||||
this.personal = personal;
|
||||
}
|
||||
|
||||
public String getEmailSmtpHost() {
|
||||
return emailSmtpHost;
|
||||
}
|
||||
|
||||
public void setEmailSmtpHost(String emailSmtpHost) {
|
||||
this.emailSmtpHost = emailSmtpHost;
|
||||
}
|
||||
|
||||
public String getEmailSmtpPort() {
|
||||
return emailSmtpPort;
|
||||
}
|
||||
|
||||
public void setEmailSmtpPort(String emailSmtpPort) {
|
||||
this.emailSmtpPort = emailSmtpPort;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getEncryption() {
|
||||
return encryption;
|
||||
}
|
||||
|
||||
public void setEncryption(String encryption) {
|
||||
this.encryption = encryption;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package cn.fateverse.common.email.entity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 邮件发送对象
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
public class SendEmailInfo {
|
||||
|
||||
/**
|
||||
* 邮件主题
|
||||
*/
|
||||
private String subject;
|
||||
/**
|
||||
* 发送对象
|
||||
*/
|
||||
private List<String> senders;
|
||||
/**
|
||||
* 抄送对象
|
||||
*/
|
||||
private List<String> ccPersons;
|
||||
/**
|
||||
* 邮件内容
|
||||
*/
|
||||
private String content;
|
||||
/**
|
||||
* 主题
|
||||
*/
|
||||
private String ThemeEnums;
|
||||
|
||||
|
||||
public SendEmailInfo() {
|
||||
}
|
||||
|
||||
|
||||
public SendEmailInfo(String subject, List<String> senders, List<String> ccPersons, String content) {
|
||||
this.subject = subject;
|
||||
this.senders = senders;
|
||||
this.ccPersons = ccPersons;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public SendEmailInfo(String subject, List<String> senders, List<String> ccPersons, String content, String themeEnums) {
|
||||
this.subject = subject;
|
||||
this.senders = senders;
|
||||
this.ccPersons = ccPersons;
|
||||
this.content = content;
|
||||
ThemeEnums = themeEnums;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public List<String> getSenders() {
|
||||
return senders;
|
||||
}
|
||||
|
||||
public void setSenders(List<String> senders) {
|
||||
this.senders = senders;
|
||||
}
|
||||
|
||||
public List<String> getCcPersons() {
|
||||
return ccPersons;
|
||||
}
|
||||
|
||||
public void setCcPersons(List<String> ccPersons) {
|
||||
this.ccPersons = ccPersons;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getThemeEnums() {
|
||||
return ThemeEnums;
|
||||
}
|
||||
|
||||
public void setThemeEnums(String themeEnums) {
|
||||
ThemeEnums = themeEnums;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.fateverse.common.email.enums;
|
||||
|
||||
/**
|
||||
* 主题枚举
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
public enum ThemeEnums {
|
||||
/**
|
||||
* 主题枚举
|
||||
*/
|
||||
DEFAULT,
|
||||
;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.fateverse.common.email.service;
|
||||
|
||||
import cn.fateverse.common.email.entity.SendEmailInfo;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
public interface EmailService {
|
||||
|
||||
/**
|
||||
* 同步发送邮件
|
||||
*
|
||||
* @param email 邮件信息
|
||||
* @return 发送结果
|
||||
*/
|
||||
boolean sendMail(SendEmailInfo email);
|
||||
|
||||
/**
|
||||
* 异步发送邮件
|
||||
*
|
||||
* @param email 邮件信息
|
||||
*/
|
||||
void asyncSendMail(SendEmailInfo email);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package cn.fateverse.common.email.service.impl;
|
||||
|
||||
import cn.fateverse.common.email.service.session.EmailSessionProvider;
|
||||
import cn.fateverse.common.email.entity.SendEmailInfo;
|
||||
import cn.fateverse.common.email.service.EmailService;
|
||||
import cn.fateverse.common.email.config.EmailProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 邮件服务默认实现
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
@Service
|
||||
public class EmailServiceImpl implements EmailService {
|
||||
|
||||
@Autowired
|
||||
private EmailProperties properties;
|
||||
|
||||
@Autowired
|
||||
private EmailSessionProvider sessionProvider;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean sendMail(SendEmailInfo email) {
|
||||
Session session = sessionProvider.getSession();
|
||||
// 创建默认的 MimeMessage 对象
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
Transport transport = null;
|
||||
try {
|
||||
message.setSubject(email.getSubject());
|
||||
message.setFrom(new InternetAddress(properties.getSender(), properties.getPersonal(), "UTF-8"));
|
||||
|
||||
InternetAddress[] senders = new InternetAddress[email.getSenders().size()];
|
||||
for (int i = 0; i < email.getSenders().size(); i++) {
|
||||
senders[i] = new InternetAddress(email.getSenders().get(i));
|
||||
}
|
||||
message.addRecipients(Message.RecipientType.TO, senders);
|
||||
if (null != email.getCcPersons() && !email.getCcPersons().isEmpty()) {
|
||||
InternetAddress[] ccSenders = new InternetAddress[email.getCcPersons().size()];
|
||||
for (int i = 0; i < email.getCcPersons().size(); i++) {
|
||||
ccSenders[i] = new InternetAddress(email.getCcPersons().get(i));
|
||||
}
|
||||
message.addRecipients(Message.RecipientType.CC, ccSenders);
|
||||
}
|
||||
message.setContent(email.getContent(), "text/html;charset=UTF-8");
|
||||
//new InternetAddress()
|
||||
message.setSentDate(new Date());
|
||||
|
||||
message.saveChanges();
|
||||
|
||||
session.setDebug(false);
|
||||
transport = session.getTransport();
|
||||
transport.connect();
|
||||
|
||||
transport.sendMessage(message, message.getAllRecipients());
|
||||
// 7. 关闭连接
|
||||
} catch (MessagingException | UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
assert transport != null;
|
||||
transport.close();
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void asyncSendMail(SendEmailInfo email) {
|
||||
sendMail(email);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.fateverse.common.email.service.session;
|
||||
|
||||
import cn.fateverse.common.email.config.EmailProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 邮件发送Session获取
|
||||
*
|
||||
* @author Clay
|
||||
* @date 2023-03-22
|
||||
*/
|
||||
public class EmailSessionProvider {
|
||||
|
||||
|
||||
@Autowired
|
||||
private EmailProperties properties;
|
||||
|
||||
private volatile Session session;
|
||||
|
||||
public Session getSession() {
|
||||
if (session == null) {
|
||||
synchronized (this) {
|
||||
if (session == null) {
|
||||
// 参数配置
|
||||
Properties props = new Properties();
|
||||
// 使用的协议(JavaMail规范要求)
|
||||
props.setProperty("mail.transport.protocol", "smtp");
|
||||
// 发件人的邮箱的 SMTP 服务器地址
|
||||
props.setProperty("mail.smtp.host", properties.getEmailSmtpHost());
|
||||
//发件人的邮箱的 SMTP 服务器端口
|
||||
props.setProperty("mail.smtp.port", properties.getEmailSmtpPort());
|
||||
// 需要请求认证
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
||||
props.setProperty("mail.smtp.socketFactory.port", properties.getEmailSmtpPort());
|
||||
session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(properties.getUsername(), properties.getPassword());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return session;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "email",
|
||||
"type": "cn.fateverse.email.config.EMailProperties",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "email.sender",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.personal",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.emailSmtpHost",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.emailSmtpPort",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.username",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.password",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
},
|
||||
{
|
||||
"name": "email.encryption",
|
||||
"type": "java.lang.String",
|
||||
"sourceType": "cn.fateverse.email.config.EMailProperties"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
cn.fateverse.common.email.config.EmailConfiguration
|
||||
Reference in New Issue
Block a user