springboot框架使用腾讯邮箱自动发送邮件

配置腾讯邮箱获取邮箱连接密码

腾讯邮箱–>设置–>账户–>打开POP3/SMTP服务–>获取授权码

SpringBoot配置

POM文件

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

application.properties配置

#这里指明发送方的邮箱
spring.mail.username=xxxx@qq.com

#password填我们刚刚说的授权码
spring.mail.password=你自己获取的授权码
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8

#下面几句是必须
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

spring.application.name=spirng-boot-mail
spring.mail.host=smtp.qq.com
默认的邮箱服务器端口
spring.mail.port=25

springboot测试程序

service层

@Service
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom("xxxxxxxxx@qq.com");//发起者
        mailMessage.setTo(to);//接受者
        //多人mailMessage.setTo("1xx.com","2xx.com","3xx.com");
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            javaMailSender.send(mailMessage);
            System.out.println("发送邮件成功");
        }catch (Exception e){
            System.out.println("发送邮件失败");
        }
    }
}

测试

 @Test
        public  void sendMailTest(){
            mailService.sendMail("xxx.com","这里是邮件的主题","助理是邮件的内容");//第一个参数是邮件的接收方
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值