使用SpringBoot發(fā)送郵箱驗證碼的簡單實現(xiàn)
題外話
日常生活中,咱們注冊某一個平臺或者找回密碼甚至是登錄到系統(tǒng)的時候,一般都需要注冊手機(jī)號,經(jīng)過手機(jī)號來接收驗證碼,然后完成這些需求。但是對于程序員來說,或許我們更加感興趣的是如何來實現(xiàn)它,但是一般這種經(jīng)過三大運營商的操作,都是需要付費的,所以咱們今天來講一種它的平替——使用QQ郵箱來發(fā)送和接收驗證碼。qq郵箱是咱們?nèi)粘J褂玫降募确奖阌置赓M的通訊工具之一(方便是因為日常使用微信,一般會和QQ郵箱關(guān)聯(lián))?,F(xiàn)在咱們來介紹一下它在SpringBoot項目中的具體應(yīng)用。
提前準(zhǔn)備
2.1 配置郵箱第三方登錄
在系統(tǒng)中使用到的郵箱發(fā)送郵件屬于第三方登錄,需要登錄QQ郵箱配置第三方登錄。
2.1.1 點擊設(shè)置——賬戶
登錄QQ郵箱,點擊設(shè)置,跳轉(zhuǎn)后找到賬戶。
2.1.2 開啟POP3/SMTP服務(wù)
在賬戶那個頁面,找到下面這一欄,點擊開始就好啦。
然后會讓你綁定郵箱的手機(jī)驗證一下:
發(fā)送完信息,就會顯示下面的授權(quán)碼(一定要保存好,很重要),復(fù)制授權(quán)碼備用。
2.2 添加依賴
回到項目,添加相關(guān)依賴,如下:
<!-- 發(fā)郵件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
tips: Spring提供了非常好用的JavaMailSender接口實現(xiàn)郵件發(fā)送。由于SpringBoot的Starter模塊也為此提供了自動化配置,所以在引入了spring-boot-starter-mail依賴之后,會根據(jù)配置文件中的內(nèi)容去創(chuàng)建JavaMailSender實例,因此我們可以直接在需要使用的地方直接@Autowired來引入郵件發(fā)送對象。
2.3 yaml配置
在這里配置好自己的郵箱和授權(quán)碼,當(dāng)然這里是自定義的,后面需要使用**@Value**獲取。
# 發(fā)送郵件配置 mail: # 發(fā)件人地址 user: 23734xxxxxx@qq.com # 發(fā)件人授權(quán)碼 password: pfemtwstpvkdabcd
進(jìn)入主題
完成前面的步驟后,我們正式寫一個發(fā)送郵件的工具類(建議直接復(fù)制)。
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * 發(fā)郵件工具類 */ @Component public final class MailUtils { @Value("${mail.user}") private String USER; // 發(fā)件人郵箱地址 @Value("${mail.password}") private String PASSWORD; // 如果是qq郵箱可以使戶端授權(quán)碼 /** * 發(fā)送郵件 * @param to 收件人郵箱 * @param text 郵件正文 * @param title 標(biāo)題 */ public boolean sendMail(String to, String text, String title){ try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); // 發(fā)件人的賬號 props.put("mail.user", USER); //發(fā)件人的密碼 props.put("mail.password", PASSWORD); // 構(gòu)建授權(quán)信息,用于進(jìn)行SMTP進(jìn)行身份驗證 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用戶名、密碼 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會話 Session mailSession = Session.getInstance(props, authenticator); // 創(chuàng)建郵件消息 MimeMessage message = new MimeMessage(mailSession); // 設(shè)置發(fā)件人 String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); // 設(shè)置收件人 InternetAddress toAddress = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, toAddress); // 設(shè)置郵件標(biāo)題 message.setSubject(title); // 設(shè)置郵件的內(nèi)容體 message.setContent(text, "text/html;charset=UTF-8"); // 發(fā)送郵件 Transport.send(message); return true; }catch (Exception e){ e.printStackTrace(); } return false; } }
這個工具類使用了**@Component**注解將這個工具類放入IOC容器中,需要使用的時候方便取。這個工具類只有一個方法——sendMail(String to, String text, String title),就是用來發(fā)郵件的方法,一共三個參數(shù),參數(shù)解釋如上有很詳細(xì)的解釋,這里就不多言。
測試使用
現(xiàn)在我們使用它來測試一下好不好使。
尾聲
3.1 安利一個生成驗證碼的工具類
3.1.1 添加依賴
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
3.1.2 編寫配置類
驗證碼的形式可以在下面改,這里是生成四位數(shù)字+字母的形式。
import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; /** * 生成驗證碼的配置 */ @Configuration public class KaptchaConfig { @Bean public Producer kaptchaProducer() { Properties properties = new Properties(); //設(shè)置驗證碼的寬度 properties.setProperty("kaptcha.image.width", "100"); //設(shè)置寬度 properties.setProperty("kaptcha.image.height", "40"); //設(shè)置字體大小 properties.setProperty("kaptcha.textproducer.font.size", "32"); //設(shè)置字體顏色 properties.setProperty("kaptcha.textproducer.font.color", "0,0,0"); //限定驗證碼中的字符 properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //設(shè)置驗證碼的長度 properties.setProperty("kaptcha.textproducer.char.length", "4"); //設(shè)置添加噪聲與否 properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); //將配置裝載到一個實例中 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); //將配置傳入實例 defaultKaptcha.setConfig(new Config(properties)); return defaultKaptcha; } }
3.1.3 使用
@Autowired private Producer checkCode; @Test void contextLoads() { String text = checkCode.createText(); System.out.println(true); }
3.1.4 演示結(jié)果
到此這篇關(guān)于使用SpringBoot發(fā)送郵箱驗證碼的簡單實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送郵箱驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐
本文主要介紹了SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10Mybatis-Plus通過SQL注入器實現(xiàn)批量插入的實踐
本文主要介紹了Mybatis-Plus通過SQL注入器實現(xiàn)批量插入的實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Windows中在IDEA上安裝和使用JetBrains Mono字體的教程
這篇文章主要介紹了Windows IDEA上安裝和使用JetBrains Mono字體的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程
Swagger2是一個開源項目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04SpringBoot中的multipartResolver上傳文件配置
這篇文章主要介紹了SpringBoot中的multipartResolver上傳文件配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10