springboot发送QQ邮箱 springboot实现发送QQ邮箱

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot发送QQ邮箱 springboot实现发送QQ邮箱

蜜桃婷婷酱   2021-06-06 我要评论
想了解springboot实现发送QQ邮箱的相关内容吗,蜜桃婷婷酱在本文为您仔细讲解springboot发送QQ邮箱的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot,QQ邮箱,springboot发送QQ邮箱,springboot发送邮箱,下面大家一起来学习吧。

springboot发送电子邮箱,供大家参考,具体内容如下

1.开启qq邮箱开启IMAP/SMTP服务*

首先进入qq邮箱

点击设置

点击账户,然后往下拉

开启IMAP/SMTP服务

开启成功得到授权密码,这个要记住,一会用

2.引入pom依赖

<!--发送邮箱-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.2.5.RELEASE</version>
</dependency>

3.配置application.properties

# QQ邮箱配置
spring.mail.host=smtp.qq.com
#发件人QQ邮箱地址
spring.mail.username=发件人的qq邮箱
#QQ邮箱授权码
spring.mail.password=得到的授权密码
#以下三项不用改动
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

4.创建发送电子邮箱controller

我这里收件人地址写死了,当然可以作为参数,访问这个方法的时候把参数加上就行了,效果都是一样的

package com.wyh.controller;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.Address;
import javax.mail.MessagingException;
import java.util.ArrayList;

@RestController
public class EmailController {
    @Resource
    private JavaMailSender javaMailSender;
    //这一步是获取application.properties中设置的发件人邮箱地址
    @Value("${spring.mail.username}")
    private String username;
   /**
    /* @Author 魏一鹤
    * @Description  发送邮箱
    * @Date 0:09 2021/5/19
    * @Param []
    * @return void
   **/
    @RequestMapping("/sendEmail")
    public String  sendMail() { //String address

        //发邮件
            SimpleMailMessage message = new SimpleMailMessage();
            //发件人邮件地址(上面获取到的,也可以直接填写,string类型)
            message.setFrom(username);
            //要发送的qq邮箱(收件人地址)
            message.setTo("1581622479@qq.com");//address
            //邮件主题
            message.setSubject("java调用QQ邮箱发送");
            //邮件正文
            message.setText("我是用java发送的QQ邮箱");//!!!
            javaMailSender.send(message);
            return "发送成功!";
    }
}

5.查看效果

在我的qq邮箱就能看到我们发送的内容了

然后换一种写法,把收件人的地址作为参数

package com.wyh.controller;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.Address;
import javax.mail.MessagingException;
import java.util.ArrayList;

@RestController
public class EmailController {
    @Resource
    private JavaMailSender javaMailSender;
    //这一步是获取application.properties中设置的发件人邮箱地址
    @Value("${spring.mail.username}")
    private String username;
   /**
    /* @Author 魏一鹤
    * @Description  发送邮箱
    * @Date 0:09 2021/5/19
    * @Param []
    * @return void
   **/
    @RequestMapping("/sendEmail")
    public String  sendMail(String address) { //String address

        //发邮件
            SimpleMailMessage message = new SimpleMailMessage();
            //发件人邮件地址(上面获取到的,也可以直接填写,string类型)
            message.setFrom(username);
            //要发送的qq邮箱(收件人地址)
            message.setTo(address);//address
            //邮件主题
            message.setSubject("java调用QQ邮箱发送");
            //邮件正文
            message.setText("我是用java发送的QQ邮箱");//!!!
            javaMailSender.send(message);
            return "发送成功!";
    }
}

效果都是一样的。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们