温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot怎么使用QQ邮箱发送邮件

发布时间:2022-03-03 14:12:11 来源:亿速云 阅读:188 作者:小新 栏目:开发技术

小编给大家分享一下springboot怎么使用QQ邮箱发送邮件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

一、QQ邮箱打开POP3/SMTP服务

springboot怎么使用QQ邮箱发送邮件
springboot怎么使用QQ邮箱发送邮件
springboot怎么使用QQ邮箱发送邮件

上面的服务开启后,会得到一串授权密码在springboot配置中需要用到

二、springboot配置

IDE目录

springboot怎么使用QQ邮箱发送邮件

1.在pom.xml添加spring-boot-starter-mail起步依赖

 <!-- springboot开发mail项目的起步依赖-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

2.在application.properties中配置mail属性

#静态资源映射,localhost:8080/ == /resources
spring.resources.static-locations=classpath:META-INF/resources/,classpath:static/,classpath:templates/

#https://blog.csdn.net/jawhiow/article/details/82625842
#如果原先访问首页的地址是:http://localhost:8888/index.html   那么在你配置这个配置后,http://localhost:8888/default/index.html
spring.mvc.static-path-pattern=/*

# 设置邮箱主机
spring.mail.host=smtp.qq.com
# 设置用户名
spring.mail.username=xxxxxx@qq.com
# 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
spring.mail.password=xxxxxx
# 设置是否需要认证,如果为true,那么用户名和密码就必须的,
# 如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
spring.mail.properties.mail.smtp.auth=true
# STARTTLS[1]  是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.port=465

3.编写controller文件

@Controller
public class EmailController {
    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("/")
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("index.html");
        response.sendRedirect("/index.html");
    }

    @ResponseBody
    @RequestMapping("/emailSend")
    public String emailSend(@RequestParam(value = "email") String eamil){
        System.out.println(eamil);
        String content = "1234";
        try{
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("xxxxxx@qq.com");
            message.setTo(eamil);
            message.setSubject("主题:主题内容");
            message.setText(content);
            //发送邮件
            javaMailSender.send(message);
            System.out.println(eamil+"发送成功");
        }catch (Exception e){
            return "fail";
        }
        return "success";
    }
}

4.编写网页页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>邮件</title>
</head>
<body>
    <form action="/emailSend">
        邮箱:<input type="text" name="email" value="xxxxxx@qq.com">
        <input type="submit">
    </form>
</body>
</html>

5.发送成功

springboot怎么使用QQ邮箱发送邮件
springboot怎么使用QQ邮箱发送邮件

以上是“springboot怎么使用QQ邮箱发送邮件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI