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
編寫兩個(gè)發(fā)送郵件的接口
package www.it.com.server;
import java.io.File;
/**
* @author wangjie:
* @version 創(chuàng)建時(shí)間: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)建時(shí)間: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對(duì)象用于處理帶有附件的郵件信息
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)建時(shí)間:2019年8月27日 上午9:52:30
* @Description 類描述:郵件發(fā)送的controller
*/
@RestController()
@RequestMapping("/mail")
public class MailController {
@Autowired
private MailServer mailServer;
/**
* 簡(jiǎn)單郵件的發(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è)置

選擇賬戶

滑動(dòng)到下面開(kāi)啟相應(yīng)的服務(wù) 選擇生成授權(quán)碼

到此這篇關(guān)于springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能的文章就介紹到這了,更多相關(guān)springboot整合mail郵箱發(fā)送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用雙異步實(shí)現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)
在開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到這樣的需求,將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)中,這篇文章主要來(lái)和大家講講Java如何使用雙異步實(shí)現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù),感興趣的可以了解下2024-01-01
基于mybatis 動(dòng)態(tài)SQL查詢總結(jié)
這篇文章主要介紹了mybatis 動(dòng)態(tài)SQL查詢總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring Boot CORS 配置方法允許跨域請(qǐng)求的最佳實(shí)踐方案
跨域請(qǐng)求在現(xiàn)代Web開(kāi)發(fā)中非常重要,特別是在涉及多個(gè)前端和后端服務(wù)時(shí),本文詳細(xì)介紹了跨域請(qǐng)求的背景、重要性以及如何解決跨域問(wèn)題,通過(guò)SpringBoot框架的CORS配置,可以有效地處理跨域請(qǐng)求,確保數(shù)據(jù)傳輸?shù)陌踩院陀脩趔w驗(yàn),感興趣的朋友跟隨小編一起看看吧2024-11-11
SpringBootTest測(cè)試時(shí)不啟動(dòng)程序的問(wèn)題
這篇文章主要介紹了SpringBootTest測(cè)試時(shí)不啟動(dòng)程序的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
maven package 打包報(bào)錯(cuò) Failed to execute goal的解決
這篇文章主要介紹了maven package 打包報(bào)錯(cuò) Failed to execute goal的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java 獲取當(dāng)前類名和方法名的實(shí)現(xiàn)方法
這篇文章主要介紹了 Java 獲取當(dāng)前類名和方法名的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供了實(shí)現(xiàn)方法并比較幾種方法的效率,需要的朋友可以參考下2017-07-07
解決JavaEE開(kāi)發(fā)中字符編碼出現(xiàn)亂碼的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決JavaEE開(kāi)發(fā)中字符編碼出現(xiàn)亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
spring源碼學(xué)習(xí)之bean的初始化以及循環(huán)引用
這篇文章主要給大家介紹了關(guān)于spring源碼學(xué)習(xí)之bean的初始化以及循環(huán)引用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

