首页>代码>SpringBoot整合mail实现邮件发送>/spring-boot-mail-demo/src/main/java/com/cxs/utils/SendMailUtil.java
package com.cxs.utils;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.Resource;
import com.cxs.config.MailConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;

/**
 * @Project: cxs-currency-sys-server
 * @Author: cxs2014501@163.com
 * @Create: 2023/2/8 13:37
 * @Description:
 **/
@Slf4j
@Component
public class SendMailUtil {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private MailConfig mailConfig;

    /**
     * 发送带附近的邮件信息
     * @param to 收件人
     * @param subject  主题
     * @param content  内容
     */
    public void sendMail(String to, String subject, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setSubject(subject);
            helper.setText(content, true);
            helper.setTo(to);
            helper.setFrom(mailConfig.getUsername());
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    /**
     * 读取邮件模板
     * 替换模板中的信息
     * @param code 内容
     * @return
     */
    public String buildCodeContent(String code) {
        //加载邮件html模板
        Resource resource = new ClassPathResource("mailtemplate.ftl");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            log.info("发送邮件读取模板失败{}", e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), "测试验证码", "全栈小白的手机号", mailConfig.getUsername(), code);
    }
}
最近下载更多
youzitao  LV11 2023年4月16日
88123hui  LV1 2023年3月28日
最代码官方  LV167 2023年2月12日
最近浏览更多
lxy135  LV1 2023年10月10日
笛卡尔积  LV6 2023年9月25日
ZhangGb 2023年9月2日
暂无贡献等级
liwiaif  LV2 2023年9月1日
lipanlong  LV10 2023年7月6日
yuyanyyqyyq 2023年6月11日
暂无贡献等级
szf123  LV12 2023年5月30日
哇塞塞哈哈哈  LV8 2023年5月4日
茫茫人海中的小牛  LV10 2023年4月23日
Mr_VVcat  LV8 2023年4月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友