欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring boot如何加入mail邮件支持

时间:2021-03-09 10:06:15|栏目:JAVA代码|点击:

这篇文章主要介绍了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

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

上一篇:Spring AOP注解案例及基本原理详解

栏    目:JAVA代码

下一篇:Linux下Java开发环境搭建以及第一个HelloWorld

本文标题:spring boot如何加入mail邮件支持

本文地址:http://www.codeinn.net/misctech/77103.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有