springboot實現(xiàn)發(fā)送QQ郵箱
springboot發(fā)送電子郵箱,供大家參考,具體內(nèi)容如下
1.開啟qq郵箱開啟IMAP/SMTP服務*
首先進入qq郵箱
點擊設置
點擊賬戶,然后往下拉
開啟IMAP/SMTP服務
開啟成功得到授權密碼,這個要記住,一會用
2.引入pom依賴
<!--發(fā)送郵箱--> <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 #發(fā)件人QQ郵箱地址 spring.mail.username=發(fā)件人的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.創(chuàng)建發(fā)送電子郵箱controller
我這里收件人地址寫死了,當然可以作為參數(shù),訪問這個方法的時候把參數(shù)加上就行了,效果都是一樣的
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中設置的發(fā)件人郵箱地址 @Value("${spring.mail.username}") private String username; /** /* @Author 魏一鶴 * @Description 發(fā)送郵箱 * @Date 0:09 2021/5/19 * @Param [] * @return void **/ @RequestMapping("/sendEmail") public String sendMail() { //String address //發(fā)郵件 SimpleMailMessage message = new SimpleMailMessage(); //發(fā)件人郵件地址(上面獲取到的,也可以直接填寫,string類型) message.setFrom(username); //要發(fā)送的qq郵箱(收件人地址) message.setTo("1581622479@qq.com");//address //郵件主題 message.setSubject("java調用QQ郵箱發(fā)送"); //郵件正文 message.setText("我是用java發(fā)送的QQ郵箱");//?。?! javaMailSender.send(message); return "發(fā)送成功!"; } }
5.查看效果
在我的qq郵箱就能看到我們發(fā)送的內(nèi)容了
然后換一種寫法,把收件人的地址作為參數(shù)
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中設置的發(fā)件人郵箱地址 @Value("${spring.mail.username}") private String username; /** /* @Author 魏一鶴 * @Description 發(fā)送郵箱 * @Date 0:09 2021/5/19 * @Param [] * @return void **/ @RequestMapping("/sendEmail") public String sendMail(String address) { //String address //發(fā)郵件 SimpleMailMessage message = new SimpleMailMessage(); //發(fā)件人郵件地址(上面獲取到的,也可以直接填寫,string類型) message.setFrom(username); //要發(fā)送的qq郵箱(收件人地址) message.setTo(address);//address //郵件主題 message.setSubject("java調用QQ郵箱發(fā)送"); //郵件正文 message.setText("我是用java發(fā)送的QQ郵箱");//?。?! javaMailSender.send(message); return "發(fā)送成功!"; } }
效果都是一樣的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式
這篇文章主要介紹了使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Springboot視頻接口報大量的ClientAbortException找不到原因的解決
本文主要介紹了Springboot視頻接口報大量的ClientAbortException找不到原因的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08MyBatis-Plus結合Layui實現(xiàn)分頁方法
MyBatis-Plus 使用簡單,本文主要介紹使用 service 中的 page 方法結合 Layui 前端框架實現(xiàn)分頁效果,具有一定的參考價值,感興趣的可以了解一下2021-08-08MyBatis-Plus?中?typeHandler?的使用實例詳解
本文介紹了在MyBatis-Plus中如何使用typeHandler處理json格式字段和自定義typeHandler,通過使用JacksonTypeHandler,可以簡單實現(xiàn)將實體類字段轉換為json格式存儲,感興趣的朋友跟隨小編一起看看吧2024-10-10