springboot 使用QQ郵箱發(fā)送郵件的操作方法
一、QQ郵箱打開POP3/SMTP服務(wù)
上面的服務(wù)開啟后,會得到一串授權(quán)密碼在springboot配置中需要用到
二、springboot配置
IDE目錄
1.在pom.xml添加spring-boot-starter-mail起步依賴
<!-- springboot開發(fā)mail項目的起步依賴--> <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 #如果原先訪問首頁的地址是:http://localhost:8888/index.html 那么在你配置這個配置后,http://localhost:8888/default/index.html spring.mvc.static-path-pattern=/* # 設(shè)置郵箱主機 spring.mail.host=smtp.qq.com # 設(shè)置用戶名 spring.mail.username=xxxxxx@qq.com # 設(shè)置密碼,該處的密碼是QQ郵箱開啟SMTP的授權(quán)碼而非QQ密碼 spring.mail.password=xxxxxx # 設(shè)置是否需要認證,如果為true,那么用戶名和密碼就必須的, # 如果設(shè)置false,可以不設(shè)置用戶名和密碼,當(dāng)然也得看你的對接的平臺是否支持無密碼進行訪問的。 spring.mail.properties.mail.smtp.auth=true # STARTTLS[1] 是對純文本通信協(xié)議的擴展。它提供一種方式將純文本連接升級為加密連接(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("主題:主題內(nèi)容"); message.setText(content); //發(fā)送郵件 javaMailSender.send(message); System.out.println(eamil+"發(fā)送成功"); }catch (Exception e){ return "fail"; } return "success"; } }
4.編寫網(wǎng)頁頁面
<!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ā)送成功
到此這篇關(guān)于springboot 使用QQ郵箱 發(fā)送郵件的文章就介紹到這了,更多相關(guān)springboot QQ郵箱 發(fā)送郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決java 分割字符串成數(shù)組時,小圓點不能直接進行分割的問題
這篇文章主要介紹了解決java 分割字符串成數(shù)組時,小圓點不能直接進行分割的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12查找native方法的本地實現(xiàn)函數(shù)native_function詳解
JDK開放給用戶的源碼中隨處可見Native方法,被Native關(guān)鍵字聲明的方法說明該方法不是以Java語言實現(xiàn)的,而是以本地語言實現(xiàn)的,Java可以直接拿來用。這里介紹下查找native方法的本地實現(xiàn)函數(shù)native_function,感興趣的朋友跟隨小編一起看看吧2021-12-12解決Eclipse配置Tomcat出現(xiàn)Cannot create a server using the selected
這篇文章主要介紹了解決Eclipse配置Tomcat出現(xiàn)Cannot create a server using the selected type錯誤的相關(guān)資料,需要的朋友可以參考下2017-02-02JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解
這篇文章主要為大家介紹了JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12