疯狂的狮子Li
2022-05-02 4941aaa5c1c59f17881d3587f1679b93736bd01e
update 重构 pr_172 集成邮件发送功能
已修改8个文件
217 ■■■■ 文件已修改
pom.xml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/resources/application-dev.yml 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/resources/application-prod.yml 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/pom.xml 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/utils/email/MailUtils.java 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/MailController.java 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/MailConfig.java 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/MailProperties.java 58 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -38,7 +38,6 @@
        <dynamic-ds.version>3.5.1</dynamic-ds.version>
        <tlog.version>1.3.6</tlog.version>
        <xxl-job.version>2.3.0</xxl-job.version>
        <mail.version>1.6.2</mail.version>
        <!-- jdk11 缺失依赖 jaxb-->
        <jaxb.version>3.0.1</jaxb.version>
ruoyi-admin/src/main/resources/application-dev.yml
@@ -137,34 +137,6 @@
    # 是否开启ssl
    ssl: false
  # 邮件
  mail:
    enabled: false
    # 邮件服务地址 (enabled = true 时打开该配置)
#    host: smtp.qq.com
    # 用户名
    username: xxx@qq.com
    # 授权码 (设置 - 账户 - POP3/SMTP服务)
    password: xxx
    # QQ邮箱加密端口,不同邮箱的端口不一样
    port: 465
    properties:
      mail:
        smtp:
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory
          ssl:
            trust: smtp.qq.com
          # 是否需要用户认证
          auth: true
          starttls:
            # 启用TLS加密
            enable: true
            required: true
    # 传输协议 starttls.enable = true 时为 smtps
    protocol: smtps
    debug: true
redisson:
  # 线程池数量
  threads: 4
@@ -184,3 +156,25 @@
    timeout: 3000
    # 发布和订阅连接池大小
    subscriptionConnectionPoolSize: 50
--- # mail 邮件发送
mail:
  enabled: false
  host: smtp.163.com
  port: 465
  # 是否需要用户名密码验证
  auth: true
  # 发送方,遵循RFC-822标准
  from: xxx@163.com
  # 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
  user: xxx@163.com
  # 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
  pass: xxxxxxxxxx
  # 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
  starttlsEnable: true
  # 使用SSL安全连接
  sslEnable: true
  # SMTP超时时长,单位毫秒,缺省值不超时
  timeout: 0
  # Socket连接超时值,单位毫秒,缺省值不超时
  connectionTimeout: 0
ruoyi-admin/src/main/resources/application-prod.yml
@@ -159,3 +159,25 @@
    timeout: 3000
    # 发布和订阅连接池大小
    subscriptionConnectionPoolSize: 50
--- # mail 邮件发送
mail:
  enabled: false
  host: smtp.163.com
  port: 465
  # 是否需要用户名密码验证
  auth: true
  # 发送方,遵循RFC-822标准
  from: xxx@163.com
  # 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
  user: xxx@163.com
  # 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
  pass: xxxxxxxxxx
  # 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
  starttlsEnable: true
  # 使用SSL安全连接
  sslEnable: true
  # SMTP超时时长,单位毫秒,缺省值不超时
  timeout: 0
  # Socket连接超时值,单位毫秒,缺省值不超时
  connectionTimeout: 0
ruoyi-common/pom.xml
@@ -128,6 +128,11 @@
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
@@ -157,12 +162,6 @@
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>lock4j-redisson-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>${mail.version}</version>
        </dependency>
    </dependencies>
ruoyi-common/src/main/java/com/ruoyi/common/utils/email/MailUtils.java
@@ -38,13 +38,13 @@
    /**
     * 获取邮件发送实例 (自定义发送人以及授权码)
     *
     * @param username 发送人
     * @param password 授权码
     * @param user 发送人
     * @param pass 授权码
     */
    public static MailAccount getMailAccount(String username, String password) {
        ACCOUNT.setFrom(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
        ACCOUNT.setUser(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
        ACCOUNT.setPass(StringUtils.blankToDefault(password, ACCOUNT.getPass()));
    public static MailAccount getMailAccount(String from, String user, String pass) {
        ACCOUNT.setFrom(StringUtils.blankToDefault(from, ACCOUNT.getFrom()));
        ACCOUNT.setUser(StringUtils.blankToDefault(user, ACCOUNT.getUser()));
        ACCOUNT.setPass(StringUtils.blankToDefault(pass, ACCOUNT.getPass()));
        return ACCOUNT;
    }
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/MailController.java
@@ -3,6 +3,8 @@
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.email.MailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
@@ -14,41 +16,34 @@
/**
 * 测试邮件发送 Controller
 * 邮件发送案例
 *
 * @author Michelle.Chung
 */
@Validated
@Api(value = "邮件控制器", tags = {"测试邮件发送"})
@Api(value = "邮件发送案例", tags = {"邮件发送案例"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RequestMapping("/demo/mail")
@RestController
public class MailController {
    /**
     * 发送邮件
     *
     * @param to      接收人
     * @param subject 标题
     * @param text    内容
     */
    @ApiOperation("发送邮件")
    @GetMapping("/sendSimpleMessage")
    public R<Void> sendSimpleMessage(String to, String subject, String text) {
        MailUtils.send(to, subject, text, false);
        return R.ok("操作成功");
    public R<Void> sendSimpleMessage(@ApiParam("接收人") String to,
                                     @ApiParam("标题") String subject,
                                     @ApiParam("内容") String text) {
        MailUtils.sendText(to, subject, text);
        return R.ok();
    }
    /**
     * 发送邮件(带附件)
     *
     * @param to      接收人
     * @param subject 标题
     * @param text    内容
     */
    @ApiOperation("发送邮件(带附件)")
    @GetMapping("/sendMessageWithAttachment")
    public R<Void> sendMessageWithAttachment(String to, String subject, String text, String filePath) {
        MailUtils.send(to, subject, text, false, new File(filePath));
        return R.ok("操作成功");
    public R<Void> sendMessageWithAttachment(@ApiParam("接收人") String to,
                                             @ApiParam("标题") String subject,
                                             @ApiParam("内容") String text,
                                             @ApiParam("附件路径") String filePath) {
        MailUtils.sendText(to, subject, text, new File(filePath));
        return R.ok();
    }
}
ruoyi-framework/src/main/java/com/ruoyi/framework/config/MailConfig.java
@@ -6,8 +6,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
 * JavaMail 配置
 *
@@ -16,23 +14,21 @@
@Configuration
public class MailConfig {
    @Resource
    private MailProperties mailProperties;
    /**
     * 初始化 JavaMailSender
     */
    @Bean
    @ConditionalOnProperty(value = "spring.mail.enabled", havingValue = "true")
    public MailAccount mailAccount() {
    @ConditionalOnProperty(value = "mail.enabled", havingValue = "true")
    public MailAccount mailAccount(MailProperties mailProperties) {
        MailAccount account = new MailAccount();
        account.setFrom(mailProperties.getUsername());
        account.setUser(mailProperties.getUsername());
        account.setPass(mailProperties.getPassword());
        account.setHost(mailProperties.getHost());
        account.setPort(mailProperties.getPort());
        account.setAuth(mailProperties.getAuth());
        account.setDebug(mailProperties.getDebug());
        account.setFrom(mailProperties.getFrom());
        account.setUser(mailProperties.getUser());
        account.setPass(mailProperties.getPass());
        account.setSocketFactoryPort(mailProperties.getPort());
        account.setStarttlsEnable(mailProperties.getStarttlsEnable());
        account.setSslEnable(mailProperties.getSslEnable());
        account.setTimeout(mailProperties.getTimeout());
        account.setConnectionTimeout(mailProperties.getConnectionTimeout());
        return account;
    }
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/MailProperties.java
@@ -1,7 +1,6 @@
package com.ruoyi.framework.config.properties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@@ -12,7 +11,7 @@
 */
@Data
@Component
@ConfigurationProperties(prefix = "spring.mail")
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
    /**
@@ -21,45 +20,52 @@
    private String enabled;
    /**
     * 邮件服务地址
     * SMTP服务器域名
     */
    private String host;
    /**
     * 用户名
     */
    private String username;
    /**
     * 授权码 (设置 - 账户 - POP3/SMTP服务)
     */
    private String password;
    /**
     * 邮箱加密端口,不同邮箱的端口不一样
     * SMTP服务端口
     */
    private Integer port;
    /**
     * 是否需要用户认证
     * 是否需要用户名密码验证
     */
    @Value("${spring.mail.properties.mail.smtp.auth}")
    private Boolean auth;
    /**
     * 是否启用TLS加密
     * 用户名
     */
    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
    private Boolean starttlsEnable;
    @Value("${spring.mail.properties.mail.smtp.ssl.trust}")
    private String sslTrust;
    private Boolean debug;
    private String user;
    /**
     * 传输协议 starttls.enable = true 时为 smtps
     * 密码
     */
    private String protocol;
    private String pass;
    /**
     * 发送方,遵循RFC-822标准
     */
    private String from;
    /**
     * 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。
     */
    private Boolean starttlsEnable;
    /**
     * 使用 SSL安全连接
     */
    private Boolean sslEnable;
    /**
     * SMTP超时时长,单位毫秒,缺省值不超时
     */
    private Long timeout;
    /**
     * Socket连接超时值,单位毫秒,缺省值不超时
     */
    private Long connectionTimeout;
}