溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot怎么使用QQ郵箱發(fā)送郵件

發(fā)布時(shí)間:2022-03-03 14:12:11 來(lái)源:億速云 閱讀:206 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下springboot怎么使用QQ郵箱發(fā)送郵件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

一、QQ郵箱打開(kāi)POP3/SMTP服務(wù)

springboot怎么使用QQ郵箱發(fā)送郵件
springboot怎么使用QQ郵箱發(fā)送郵件
springboot怎么使用QQ郵箱發(fā)送郵件

上面的服務(wù)開(kāi)啟后,會(huì)得到一串授權(quán)密碼在springboot配置中需要用到

二、springboot配置

IDE目錄

springboot怎么使用QQ郵箱發(fā)送郵件

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

 <!-- springboot開(kāi)發(fā)mail項(xiàng)目的起步依賴-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

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

#靜態(tài)資源映射,localhost:8080/ == /resources
spring.resources.static-locations=classpath:META-INF/resources/,classpath:static/,classpath:templates/

#https://blog.csdn.net/jawhiow/article/details/82625842
#如果原先訪問(wèn)首頁(yè)的地址是:http://localhost:8888/index.html   那么在你配置這個(gè)配置后,http://localhost:8888/default/index.html
spring.mvc.static-path-pattern=/*

# 設(shè)置郵箱主機(jī)
spring.mail.host=smtp.qq.com
# 設(shè)置用戶名
spring.mail.username=xxxxxx@qq.com
# 設(shè)置密碼,該處的密碼是QQ郵箱開(kāi)啟SMTP的授權(quán)碼而非QQ密碼
spring.mail.password=xxxxxx
# 設(shè)置是否需要認(rèn)證,如果為true,那么用戶名和密碼就必須的,
# 如果設(shè)置false,可以不設(shè)置用戶名和密碼,當(dāng)然也得看你的對(duì)接的平臺(tái)是否支持無(wú)密碼進(jìn)行訪問(wèn)的。
spring.mail.properties.mail.smtp.auth=true
# STARTTLS[1]  是對(duì)純文本通信協(xié)議的擴(kuò)展。它提供一種方式將純文本連接升級(jí)為加密連接(TLS或SSL),而不是另外使用一個(gè)端口作加密通信。
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("主題:主題內(nèi)容");
            message.setText(content);
            //發(fā)送郵件
            javaMailSender.send(message);
            System.out.println(eamil+"發(fā)送成功");
        }catch (Exception e){
            return "fail";
        }
        return "success";
    }
}

4.編寫網(wǎng)頁(yè)頁(yè)面

<!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.發(fā)送成功

springboot怎么使用QQ郵箱發(fā)送郵件
springboot怎么使用QQ郵箱發(fā)送郵件

以上是“springboot怎么使用QQ郵箱發(fā)送郵件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI