springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能
第一步添加mail的依賴
<!--引入mail的依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
第二步編寫郵箱的
yml配置文件
spring: #郵箱配置 mail: host: smtp.qq.com username: 2631245486@qq.com #QQ郵箱的授權(quán)碼 password: 授權(quán)碼 default-encoding: UTF-8 properties: mail: smtp: auth: true starttls: enable: true required: true
properties的配置文件
#qq郵箱配置 # JavaMailSender 郵件發(fā)送的配置 spring.mail.host=smtp.qq.com spring.mail.username=用戶qq郵箱 #QQ郵箱的授權(quán)碼 spring.mail.password=授權(quán)碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.default-encoding=UTF-8 #163郵箱配置 spring.mail.host=smtp.163.com spring.mail.username=用戶163郵箱 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 spring.mail.default-encoding=UTF-8
編寫兩個發(fā)送郵件的接口
package www.it.com.server; import java.io.File; /** * @author wangjie: * @version 創(chuàng)建時間:2019年8月27日 上午10:13:08 * @Description 類描述: */ public interface MailServer { /** * @param sendUser 郵件接收人 * @param title 郵件的標(biāo)題 * @param text 郵件的內(nèi)容 */ void sendMailServer(String sendUser,String title,String text); /** * 帶有附件郵箱的發(fā)送 * @param sendUser * @param title * @param text * @param file */ void sendFileMail(String sendUser,String title,String text,File file); }
接口的實(shí)現(xiàn)
package www.it.com.server.impl; import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.logging.log4j.message.SimpleMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import www.it.com.server.MailServer; /** * @author wangjie: * @version 創(chuàng)建時間:2019年8月27日 上午10:13:58 * @Description 類描述: */ @Service public class MailServerImpl implements MailServer { @Value("${spring.mail.username}") private String fromUser; @Autowired private JavaMailSender javaMailSender; public String getFromUser() { return fromUser; } public void setFromUser(String fromUser) { this.fromUser = fromUser; } @Override public void sendMailServer(String sendUser, String title, String text) { //創(chuàng)建郵件的實(shí)體 用于封裝發(fā)送郵件需要的信息 SimpleMailMessage simpleMailMessage=new SimpleMailMessage(); //郵件的發(fā)送人 simpleMailMessage.setFrom(fromUser); //郵件接收人 simpleMailMessage.setTo(sendUser); //郵件的標(biāo)題 simpleMailMessage.setSubject(title); //郵件的內(nèi)容 simpleMailMessage.setText(text); //發(fā)送郵件 javaMailSender.send(simpleMailMessage); } @Override public void sendFileMail(String sendUser, String title, String text, File file) { MimeMessage mimeMessage = null; try { mimeMessage =javaMailSender.createMimeMessage(); //創(chuàng)建mimeMessageHelper對象用于處理帶有附件的郵件信息 MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true); mimeMessageHelper.setFrom(fromUser); mimeMessageHelper.setTo(sendUser); mimeMessageHelper.setSubject(title); mimeMessageHelper.setText(text); FileSystemResource r = new FileSystemResource(file); //添加附件 mimeMessageHelper.addAttachment("附件", r); javaMailSender.send(mimeMessage); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
controller編碼
package www.it.com.controller; import java.io.File; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import www.it.com.server.MailServer; /** * @author wangjie: * @version 創(chuàng)建時間:2019年8月27日 上午9:52:30 * @Description 類描述:郵件發(fā)送的controller */ @RestController() @RequestMapping("/mail") public class MailController { @Autowired private MailServer mailServer; /** * 簡單郵件的發(fā)送 * @return */ @RequestMapping("/send") public String sendMail() { //2694433816 mailServer.sendMailServer("2631245486@qq.com", "你好", "明天去你家玩"); return "success"; } /** * 發(fā)送帶有附件的郵件 */ @RequestMapping("/sendFile") public String sendFileMail() { File file=new File("C://Users//DELL//Desktop//學(xué)習(xí)資料.txt"); mailServer.sendFileMail("2631245486@qq.com", "你好dsf", "這是第二封帶有附件的郵件", file); return "success"; } }
授權(quán)碼生成的步驟
登錄郵箱選擇設(shè)置
選擇賬戶
滑動到下面開啟相應(yīng)的服務(wù) 選擇生成授權(quán)碼
到此這篇關(guān)于springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能的文章就介紹到這了,更多相關(guān)springboot整合mail郵箱發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用MUI框架構(gòu)建App請求http接口實(shí)例代碼
下面小編就為大家分享一篇使用MUI框架構(gòu)建App請求http接口實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
這篇文章主要介紹了用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用,本文通過實(shí)例代碼詳解給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03SpringBoot整合Swagger和Actuator的使用教程詳解
Swagger 是一套基于 OpenAPI 規(guī)范構(gòu)建的開源工具,可以幫助我們設(shè)計、構(gòu)建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項(xiàng)目監(jiān)控)使用教程。感興趣的朋友一起看看吧2019-06-06使用Flyway進(jìn)行Java數(shù)據(jù)庫版本控制的操作指南
今天我們將深入探討如何使用Flyway進(jìn)行Java數(shù)據(jù)庫版本控制,Flyway是一個流行的數(shù)據(jù)庫遷移工具,用于管理和自動化數(shù)據(jù)庫模式的演變,文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07完美解決springboot項(xiàng)目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題(圖文詳解)
這篇文章主要介紹了完美解決springboot項(xiàng)目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Java并發(fā)編程之ReentrantLock可重入鎖的實(shí)例代碼
這篇文章主要介紹了Java并發(fā)編程之ReentrantLock可重入鎖的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02