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

spring boot如何加入mail郵件支持

 更新時(shí)間:2019年12月10日 11:02:33   作者:默聞120  
這篇文章主要介紹了spring boot如何加入mail郵件支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了spring boot如何加入mail郵件支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、添加依賴

<!-- 郵件整合 -->
		<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ù):開啟服務(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)然也得看你的對接的平臺是否支持無密碼進(jìn)行訪問的。
spring.mail.properties.mail.smtp.auth=true
#STARTTLS[1] 是對純文本通信協(xié)議的擴(kuò)展。它提供一種方式將純文本連接升級為加密連接(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;

使用MailConfig里面的方法發(fā)送即可以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論