springboot通过qq邮箱发送邮件

首先:导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application.yml里面配置

  mail:
    username: 。。。。@qq.com # 发送人邮箱
    password: klxrnmvozgv # 邮箱授权码(上面获取到的)
    host: smtp.qq.com # 邮件服务器,163邮箱就是smtp.163.com
    protocol: smtp # 协议,可以不配,默认就是这个

发送邮件的方法,sevrice

package com.zjtx.email.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮件发送工具类
 * 
 * @author wuxiaobing
 * @date 2022年10月21日13:10
 */
@Component
public class EmailUtils {

    // 发送邮件服务
    @Autowired
    private JavaMailSender javaMailSender;
    // 发送者
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单文本邮件
     * @param subject 主题
     * @param content 内容
     * @param to      收件人列表
     * @author BillDowney
     * @date 2022年10月21日13:35
     */
    public void sendSimpleMail(String subject, String content, String... to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(content);
        message.setTo(to);
        javaMailSender.send(message);
    }

}

controller类

package com.zjtx.email.controller;

import com.zjtx.email.utils.EmailUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wuxiaobing
 * @date 2022年10月21日14:12
 * @description:
 */
@RestController
@RequestMapping("email")
public class EmailTestController {

    @Resource
    private EmailUtils emailUtils;

    @GetMapping("/sendMailTest")
    public String sendMailTest() {
        emailUtils.sendSimpleMail("测试邮件发送", "这是一封测试邮件的内容.......", "xxx@126.com");
        return "mail send success";
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大眼眉毛不秃头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值