温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot加入mail邮件支持的方法

发布时间:2021-02-02 10:46:47 来源:亿速云 阅读:237 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关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 boot加入mail邮件支持的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI