欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合mail實現(xiàn)郵箱的發(fā)送功能

 更新時間:2021年09月06日 17:30:32   作者:求知若渴的蝸牛  
本文分步驟給大家介紹springboot整合mail實現(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郵箱的授權碼
    password: 授權碼
    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郵箱的授權碼
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

#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 郵件的標題 
	 * @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);
}

接口的實現(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)建郵件的實體 用于封裝發(fā)送郵件需要的信息
		SimpleMailMessage simpleMailMessage=new  SimpleMailMessage();
		//郵件的發(fā)送人
		simpleMailMessage.setFrom(fromUser);
		//郵件接收人
		simpleMailMessage.setTo(sendUser);
		//郵件的標題
		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//學習資料.txt");
		mailServer.sendFileMail("2631245486@qq.com", "你好dsf", "這是第二封帶有附件的郵件", file);
		return "success";
	}
}

授權碼生成的步驟

登錄郵箱選擇設置

選擇賬戶

滑動到下面開啟相應的服務 選擇生成授權碼

到此這篇關于springboot整合mail實現(xiàn)郵箱的發(fā)送功能的文章就介紹到這了,更多相關springboot整合mail郵箱發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用MUI框架構建App請求http接口實例代碼

    使用MUI框架構建App請求http接口實例代碼

    下面小編就為大家分享一篇使用MUI框架構建App請求http接口實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 利用JavaMail發(fā)送HTML模板郵件

    利用JavaMail發(fā)送HTML模板郵件

    這篇文章主要為大家詳細介紹了利用JavaMail發(fā)送HTML模板郵件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 使用Prometheus+Grafana的方法監(jiān)控Springboot應用教程詳解

    使用Prometheus+Grafana的方法監(jiān)控Springboot應用教程詳解

    這篇文章主要介紹了用Prometheus+Grafana的方法監(jiān)控Springboot應用,本文通過實例代碼詳解給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot整合Swagger和Actuator的使用教程詳解

    SpringBoot整合Swagger和Actuator的使用教程詳解

    Swagger 是一套基于 OpenAPI 規(guī)范構建的開源工具,可以幫助我們設計、構建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項目監(jiān)控)使用教程。感興趣的朋友一起看看吧
    2019-06-06
  • 使用Flyway進行Java數(shù)據(jù)庫版本控制的操作指南

    使用Flyway進行Java數(shù)據(jù)庫版本控制的操作指南

    今天我們將深入探討如何使用Flyway進行Java數(shù)據(jù)庫版本控制,Flyway是一個流行的數(shù)據(jù)庫遷移工具,用于管理和自動化數(shù)據(jù)庫模式的演變,文中通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • 完美解決springboot項目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題(圖文詳解)

    完美解決springboot項目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題(圖文詳解)

    這篇文章主要介紹了完美解決springboot項目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • 詳解SpringBoot中時間類型的序列化與反序列化

    詳解SpringBoot中時間類型的序列化與反序列化

    這篇文章主要為大家詳細介紹了SpringBoot中時間類型的序列化與反序列化的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-02-02
  • Java并發(fā)編程之ReentrantLock可重入鎖的實例代碼

    Java并發(fā)編程之ReentrantLock可重入鎖的實例代碼

    這篇文章主要介紹了Java并發(fā)編程之ReentrantLock可重入鎖的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Spring中bean對象的裝配方式、作用域及生命周期詳解

    Spring中bean對象的裝配方式、作用域及生命周期詳解

    這篇文章主要介紹了Spring中bean對象的裝配方式、作用域及生命周期詳解,SprignBoot中?@Bean?完美的替換了了上面的這種在xml中配置的方法,使用以下方法就能讓spring在需要自動創(chuàng)建Info對象時,自動調(diào)用這個方法,需要的朋友可以參考下
    2023-11-11
  • 深入解析spring?AOP原理及源碼

    深入解析spring?AOP原理及源碼

    這篇文章主要介紹了spring?AOP原理及源碼分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒,需要的朋友可以參考下
    2022-04-04

最新評論