SpringBoot实现发送qq邮件

2022-10-30 14:56:43 浏览数 (1)

前言

当自己的文章有新的评论的时候,如果能有个方式通知是不是很赞呢!

下面就介绍一下我是如何实现评论通知的吧!

准备工作

1、获取授权码,页面首部找到设置

2、账户 - 开启PO、P3/SMTP 服务

3、获取授权码

实现

1、添加依赖

代码语言:javascript复制
<!-- springboot 邮件mail -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>${springboot.version}</version>
</dependency>

2、配置YML

代码语言:javascript复制
spring:
  mail:
    #邮箱编码
    default-encoding: utf-8
    # 配置 SMTP 服务器地址
    host: smtp.qq.com
    #发送者邮箱
    username: xxxxx@qq.com
    #授权码
    password: xxxxxxxxxx
    #端口号
    port: 587

3、代码实现

代码语言:javascript复制
@Data
public class MailRequest implements Serializable {
    /**
     * 接收人
     */
    private String sendTo;

    /**
     *  邮件主题
     */
    private String subject;

    /**
     *  邮件内容
     */
    private String text;

    /**
     *  附件路径
     */
    private String filePath;

}
代码语言:javascript复制
public interface SendMailService {

    /**
     * 简单文本邮件

     * @param mailRequest
     * @return
     */
    void sendSimpleMail(MailRequest mailRequest);


    /**
     * Html格式邮件,可带附件

     * @param mailRequest
     * @return
     */
    void sendHtmlMail(MailRequest mailRequest);
}
代码语言:javascript复制
@Slf4j
@Service

public class SendMailServiceImpl implements SendMailService {

    //注入邮件工具类
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String sendMailer;



    @Override
    public void sendSimpleMail(MailRequest mailRequest) {
        SimpleMailMessage message = new SimpleMailMessage();

        //邮件发件人
        message.setFrom(sendMailer);
        //邮件收件人 1或多个
        message.setTo(mailRequest.getSendTo().split(","));
        //邮件主题
        message.setSubject(mailRequest.getSubject());
        //邮件内容
        message.setText(mailRequest.getText());
        //邮件发送时间
        message.setSentDate(new Date());

        javaMailSender.send(message);

        log.info("发送邮件成功");

    }



    @Override
    public void sendHtmlMail(MailRequest mailRequest) {
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            //邮件发件人
            helper.setFrom(sendMailer);
            //邮件收件人 1或多个
            helper.setTo(mailRequest.getSendTo().split(","));
            //邮件主题
            helper.setSubject(mailRequest.getSubject());
            //邮件内容
            helper.setText(mailRequest.getText(),true);
            //邮件发送时间
            helper.setSentDate(new Date());

            String filePath = mailRequest.getFilePath();
            if (StringUtils.hasText(filePath)) {
                FileSystemResource file = new FileSystemResource(new File(filePath));
                String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
                helper.addAttachment(fileName,file);
            }
            javaMailSender.send(message);
            log.info("发送邮件成功");

        } catch (MessagingException e) {
            log.error("发送邮件时发生异常!",e);
        }
    }
}
代码语言:javascript复制
@RestController

@Api(value = "发送邮件接口",tags = {"发送邮件接口"})
public class SendMailController {
    @Autowired
    private SendMailService sendMailService;

    @PostMapping("/simple")
    public void SendSimpleMessage(@RequestBody MailRequest mailRequest) {
        sendMailService.sendSimpleMail(mailRequest);
    }

    @PostMapping("/html")
    public void SendHtmlMessage(@RequestBody MailRequest mailRequest) { sendMailService.sendHtmlMail(mailRequest);}
}

到此就已经实现,根据自己的需求改动代码,下面是本站的评论通知效果,自定义的html代码。

0 人点赞