springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能
第一步添加mail的依賴(lài)
<!--引入mail的依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
第二步編寫(xiě)郵箱的
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=用戶(hù)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=用戶(hù)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
編寫(xiě)兩個(gè)發(fā)送郵件的接口
package www.it.com.server;
import java.io.File;
/**
* @author wangjie:
* @version 創(chuàng)建時(shí)間:2019年8月27日 上午10:13:08
* @Description 類(lèi)描述:
*/
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 類(lèi)描述:
*/
@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 類(lèi)描述:郵件發(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è)置

選擇賬戶(hù)

滑動(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)文章
使用MUI框架構(gòu)建App請(qǐng)求http接口實(shí)例代碼
下面小編就為大家分享一篇使用MUI框架構(gòu)建App請(qǐng)求http接口實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
這篇文章主要介紹了用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用,本文通過(guò)實(shí)例代碼詳解給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot整合Swagger和Actuator的使用教程詳解
Swagger 是一套基于 OpenAPI 規(guī)范構(gòu)建的開(kāi)源工具,可以幫助我們?cè)O(shè)計(jì)、構(gòu)建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項(xiàng)目監(jiān)控)使用教程。感興趣的朋友一起看看吧2019-06-06
使用Flyway進(jìn)行Java數(shù)據(jù)庫(kù)版本控制的操作指南
今天我們將深入探討如何使用Flyway進(jìn)行Java數(shù)據(jù)庫(kù)版本控制,Flyway是一個(gè)流行的數(shù)據(jù)庫(kù)遷移工具,用于管理和自動(dòng)化數(shù)據(jù)庫(kù)模式的演變,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
完美解決springboot項(xiàng)目出現(xiàn)”java: 錯(cuò)誤: 無(wú)效的源發(fā)行版:17“問(wèn)題(圖文詳解)
這篇文章主要介紹了完美解決springboot項(xiàng)目出現(xiàn)”java: 錯(cuò)誤: 無(wú)效的源發(fā)行版:17“問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
詳解SpringBoot中時(shí)間類(lèi)型的序列化與反序列化
這篇文章主要為大家詳細(xì)介紹了SpringBoot中時(shí)間類(lèi)型的序列化與反序列化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
Java并發(fā)編程之ReentrantLock可重入鎖的實(shí)例代碼
這篇文章主要介紹了Java并發(fā)編程之ReentrantLock可重入鎖的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解
這篇文章主要介紹了Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解,SprignBoot中?@Bean?完美的替換了了上面的這種在xml中配置的方法,使用以下方法就能讓spring在需要自動(dòng)創(chuàng)建Info對(duì)象時(shí),自動(dòng)調(diào)用這個(gè)方法,需要的朋友可以參考下2023-11-11

