spring boot 加入mail郵件支持


一、添加依賴

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

二、添加mail.properties配置文件

#設置郵箱主機
spring.mail.host=smtp.qq.com
#設置用戶名
spring.mail.username=xxxxxxx
#設置密碼
#QQ郵箱->設置->賬戶->POP3/SMTP服務:開啟服務后會獲得QQ的授權碼
spring.mail.password=xxxxxxxxxxxxxxxx
#端口
spring.mail.port=465
#協議
#spring.mail.protocol=smtp
#設置是否需要認證,如果為true,那么用戶名和密碼就必須的,
#如果設置false,可以不設置用戶名和密碼,當然也得看你的對接的平台是否支持無密碼進行訪問的。
spring.mail.properties.mail.smtp.auth=true
#STARTTLS[1]  是對純文本通信協議的擴展。它提供一種方式將純文本連接升級為加密連接(TLS或SSL),而不是另外使用一個端口作加密通信。
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

三、加載屬性文件

在啟動類加上

@PropertySource({ "classpath:mail.properties" })

 

四、添加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;

	/**
	 * 發送純文本形式的email
	 *
	 * @param toEmail 收件人郵箱
	 * @param title   郵件標題
	 * @param content 郵件內容
	 */
	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);
	}

	/**
	 * 發送帶有html的內容
	 *
	 * @param toEmail     收件人郵箱
	 * @param title       郵件標題
	 * @param htmlContent 郵件內容
	 */
	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發送
	 *
	 * @param toEmail    收件人地址
	 * @param title      郵件標題
	 * @param content    文本內容
	 * @param aboutFiles 附件信息 每個子項都是一個文件相關信息的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里面的方法發送即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM