springboot加入mail郵件支持的方法

這篇文章給大家分享的是有關(guān)spring boot加入mail郵件支持的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

10年積累的成都做網(wǎng)站、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有柳北免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

一、添加依賴

<!-- 郵件整合 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>

二、添加mail.properties配置文件

#設(shè)置郵箱主機(jī)
spring.mail.host=smtp.qq.com
#設(shè)置用戶名
spring.mail.username=xxxxxxx
#設(shè)置密碼
#QQ郵箱->設(shè)置->賬戶->POP3/SMTP服務(wù):開(kāi)啟服務(wù)后會(huì)獲得QQ的授權(quán)碼
spring.mail.password=xxxxxxxxxxxxxxxx
#端口
spring.mail.port=465
#協(xié)議
#spring.mail.protocol=smtp
#設(shè)置是否需要認(rèn)證,如果為true,那么用戶名和密碼就必須的,
#如果設(shè)置false,可以不設(shè)置用戶名和密碼,當(dāng)然也得看你的對(duì)接的平臺(tái)是否支持無(wú)密碼進(jìn)行訪問(wèn)的。
spring.mail.properties.mail.smtp.auth=true
#STARTTLS[1] 是對(duì)純文本通信協(xié)議的擴(kuò)展。它提供一種方式將純文本連接升級(jí)為加密連接(TLS或SSL),而不是另外使用一個(gè)端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

三、添加MailConfig.java

package com.spring.config;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

@Configuration
public class MailConfig {

	@Resource
	private JavaMailSenderImpl mailSender;

	@Value("${spring.mail.username}")
	private String username;

	/**
	 * 發(fā)送純文本形式的email
	 *
	 * @param toEmail 收件人郵箱
	 * @param title  郵件標(biāo)題
	 * @param content 郵件內(nèi)容
	 */
	public void sendTextMail(String toEmail, String title, String content) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom(username);
		msg.setTo(toEmail);
		msg.setSubject(title);
		msg.setText(content);
		mailSender.send(msg);
	}

	/**
	 * 發(fā)送帶有html的內(nèi)容
	 *
	 * @param toEmail   收件人郵箱
	 * @param title    郵件標(biāo)題
	 * @param htmlContent 郵件內(nèi)容
	 */
	public void sendHtmlMail(String toEmail, String title, String htmlContent) throws MessagingException {
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");
		helper.setFrom(username);
		helper.setTo(toEmail);
		helper.setSubject(title);
		helper.setText(htmlContent, true);
		mailSender.send(msg);
	}

	/**
	 * 添加附件的email發(fā)送
	 *
	 * @param toEmail  收件人地址
	 * @param title   郵件標(biāo)題
	 * @param content  文本內(nèi)容
	 * @param aboutFiles 附件信息 每個(gè)子項(xiàng)都是一個(gè)文件相關(guān)信息的map Map<String,String>: 1.filePath
	 *          2.fileName
	 * @throws Exception 異常
	 */
	public void sendAttachmentMail(String toEmail, String title, String content, List<Map<String, String>> aboutFiles) throws Exception {
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
		helper.setFrom(username);
		helper.setTo(toEmail);
		helper.setSubject(title);
		helper.setText(content);
		FileSystemResource resource = null;
		for (Map<String, String> file : aboutFiles) {
			resource = new FileSystemResource(file.get("filePath"));
			if (resource.exists()) {// 是否存在資源
				File attachmentFile = resource.getFile();
				helper.addAttachment(file.get("fileName"), attachmentFile);
			}
		}
		mailSender.send(msg);
	}

}

四、使用MailConfig

@Autowired
private MailConfig mailConfig;

感謝各位的閱讀!關(guān)于“spring boot加入mail郵件支持的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

當(dāng)前標(biāo)題:springboot加入mail郵件支持的方法
URL網(wǎng)址:http://bm7419.com/article38/gejcsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、品牌網(wǎng)站制作、外貿(mào)建站、云服務(wù)器、靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)