spring boot如何加入mail邮件支持

网友投稿 657 2023-07-06

spring boot如何加入mail邮件支持

spring boot如何加入mail邮件支持

这篇文章主要介绍了spring boot如何加入mail邮件支持,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、添加依赖

org.springframework.boot

spring-boot-starter-mail

二、添加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-.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, "uUeRHbvkRtf-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: 1.filePath

* 2.fileName

* @throws Exception 异常

*/

public void sendAttachmentMail(String toEmail, String title, String content, List> 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 file : aboutFiles) {

resource = new FileSystemResource(file.get("filePath"));

if (resource.exists()) {// 是否存在资源

File attachmentFile = resource.getFile();

helper.addAttachment(file.get("fileName"), attachmentFile);

}

}

mailSender.send(msg);

UeRHbvkR}

}

四、使用MailConfig

@Autowired

private MailConfig mailConfig;

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringCloud Feign参数问题及解决方法
下一篇:RabbitMQ简单队列实例及原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~