yupeng
2025-02-26 0874027a9170aa69ff256ae4f11e1fbbbc5705fc
feat: 新增邮件发送工具类
2个文件已修改
2个文件已添加
128 ■■■■■ 已修改文件
ruoyi-common/pom.xml 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/config/MailProperties.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/config/SmsConfig.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java 99 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/pom.xml
@@ -200,7 +200,11 @@
            <artifactId>hutool-all</artifactId>
            <version>5.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version> <!-- 请检查是否有更新的版本 -->
        </dependency>
    </dependencies>
</project>
ruoyi-common/src/main/java/com/ruoyi/common/config/MailProperties.java
New file
@@ -0,0 +1,20 @@
package com.ruoyi.common.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
    private String smtpHost="gz-smtp.qcloudmail.com";
    private Integer smtpPort = 465;
    private String userAddr = "test@xzgtmail.591taxi.cn";
    private String password = "CY20250226pass";
    private String userName = "测试";
}
ruoyi-common/src/main/java/com/ruoyi/common/config/SmsConfig.java
@@ -10,7 +10,7 @@
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(SmsProperties.class)
@EnableConfigurationProperties({SmsProperties.class, MailProperties.class})
@ConditionalOnProperty(value = SmsProperties.ENABLE_KEY,matchIfMissing = true)
public class SmsConfig {
@@ -33,6 +33,5 @@
        // 实例化要请求产品(sms)的client对象,第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
        return new SmsClient(cred, "ap-guangzhou", clientProfile);
    }
}
ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java
New file
@@ -0,0 +1,99 @@
package com.ruoyi.common.utils;
import com.ruoyi.common.config.MailProperties;
import com.ruoyi.common.exception.ServiceException;
import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Component
@Slf4j
public class TencentMailUtil {
    @Autowired
    MailProperties properties;
    public void send(String emailAddress,String param){
        // 配置发送邮件的环境属性
        final Properties props = new Properties();
        // 表示SMTP发送邮件,需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", properties.getSmtpHost());
        // 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", properties.getSmtpPort());
        props.put("mail.smtp.port", properties.getSmtpPort());
        // 发件人的账号,填写控制台配置的发信地址,比如xxx@xxx.com
        props.put("mail.user", properties.getUserAddr());
        // 访问SMTP服务时需要提供的密码(在控制台选择发信地址进行设置)
        props.put("mail.password", properties.getPassword());
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.enable", "false");
        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
//        mailSession.setDebug(true);
        //UUID uuid = UUID.randomUUID();
        //final String messageIDValue = "<" + uuid.toString() + ">";
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession) {
            //@Override
            //protected void updateMessageID() throws MessagingException {
            //设置自定义Message-ID值
            //setHeader("Message-ID", messageIDValue);
            //}
        };
        try {
            // 设置发件人邮件地址和名称。填写控制台配置的发信地址,比如xxx@xxx.com。和上面的mail.user保持一致。名称用户可以自定义填写。
            InternetAddress from = new InternetAddress(properties.getUserAddr(), properties.getUserName());
            message.setFrom(from);
            //可选。设置回信地址
//            Address[] a = new Address[1];
//            a[0] = new InternetAddress("***");
//            message.setReplyTo(a);
            // 设置收件人邮件地址,比如yyy@yyy.com
            InternetAddress to = new InternetAddress(emailAddress);
            message.setRecipient(MimeMessage.RecipientType.TO, to);
            //如果同时发给多人,才将上面两行替换为如下(因为部分收信系统的一些限制,尽量每次投递给一个人;同时我们限制单次允许发送的人数是50人):
            // 设置邮件标题
            message.setSubject("您的"+param+"账单提醒");
            message.setHeader("Content-Transfer-Encoding", "base64");
            // 设置邮件的内容体 type: text/plain(纯文本)text/html(HTML 文档)
            message.setContent("邻居您好!您"+param+",提醒您有账单需要处理,如已处理请忽略此短信,感谢您的支持!如有疑问请详询:4008888888。", "text/html;charset=UTF-8");
            //发送邮件
            Transport.send(message);
        } catch (MessagingException | UnsupportedEncodingException e) {
            log.error("发送邮件发生异常",e);
            throw new ServiceException("发送邮件失败,请检查");
        }
    }
//    public static void main(String[] args) {
//        TencentMailUtil tencentMailUtil = new TencentMailUtil();
//        MailProperties properties = new MailProperties();
//        tencentMailUtil.properties = properties;
//        tencentMailUtil.send("214491528@qq.com","大学城揽院24栋");
//
//    }
}