Spring Boot——集成spring-boot-starter-mail发送163邮箱|QQ邮箱|Gmail邮箱邮件

前言

发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息。正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在Spring Boot提供了一套集成spring-boot-starter-mail的更简易使用的封装。

基本概念

spring-boot-starter-mail

Spring框架提供了一个有用的实用程序库,用于发送电子邮件,使您免受底层邮件系统的限制,并负责代表客户端进行低级资源处理。

org.springframework.mail软件包是Spring框架的电子邮件支持的根级软件包。用于发送电子邮件的中央界面是该MailSender 界面。封装了简单邮件(例如fromto,以及许多其他邮件)的属性的简单值对象是SimpleMailMessage类。此程序包还包含一个已检查异常的层次结构,该层次结构提供了比较低级别的邮件系统异常更高的抽象级别,根异常为 MailException

官方文档

Spring Boot:https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/htmlsingle/#boot-features-email 

Spring:https://docs.spring.io/spring/docs/5.2.3.RELEASE/spring-framework-reference/integration.html#mail

Maven

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

 依赖树:

spring-boot-starter-mail-xxx.jar对Sun公司的邮件api功能进行了相应的封装。 

:Sun公司的邮件API不在Java SE JDK中,需要下载Java EE JDK & Java EE SDK

(实际上Java EE是在Java SE基础上发展构建的,是一系列技术规范,官方提供的Java EE SDK是Java EE的参考实现,是实现Java EE最全的开发工具包,而我们常说的JDK只是包含Java SE API实现,Java SE中存在与Java EE有关的规范;Java EE 7主要包括下面一些技术规范:http://www.oracle.com/technetwork/cn/java/javaee/tech/index.html

Java EE JDK Specificationhttps://jcp.org/aboutJava/communityprocess/final/jsr366/index.html

Java EE SDKhttps://www.oracle.com/java/technologies/java-ee-sdk-download.html

 

配置 

application.properties配置

#Email Config
spring.mail.test-connection=true
# 设置邮箱主机
spring.mail.host=smtp.qq.com
spring.mail.port=587
# 设置用户名
spring.mail.username=xxxxxxxx@qq.com
# 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
spring.mail.password=xxxxxxxxxxxx
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
# 设置是否需要认证,如果为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.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false

 application.yml配置 

spring:
	mail:
    	# 163
    	host: smtp.163.com
    	port:
    	username: yimcarson@163.com
    	password: ************
    	protocol: smtp
    	default-encoding: UTF-8
    	properties:
      		mail.smtp.auth: true
      		mail.smtp.starttls.enable: true
      		mail.smtp.starttls.required: true
      		mail.smtp.socketFactory.port: 465
      		mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
      		mail.smtp.socketFactory.fallback: false

Mail自动配置类: MailSenderAutoConfiguration(了解)

Spring Boot对Mail功能已经配置了相关的基本配置信息,它是Spring Boot官方提供,其类为MailSenderAutoConfiguration

//MailSenderAutoConfiguration
@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import(JndiSessionConfiguration.class)
public class MailSenderAutoConfiguration {
 
    private final MailProperties properties;
 
	private final Session session;
 
	public MailSenderAutoConfiguration(MailProperties properties,
			ObjectProvider<Session> session) {
		this.properties = properties;
		this.session = session.getIfAvailable();
	}
 
	@Bean
	public JavaMailSenderImpl mailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		if (this.session != null) {
			sender.setSession(this.session);
		}
		else {
			applyProperties(sender);
		}
		return sender;
	}
    
    //other code...
}

在MailSenderAutoConfiguration自动配置类中,创建了一个Bean,其类为JavaMailSenderImpl,它是Spring专门用来发送Mail邮件的服务类,SpringBoot也使用它来发送邮件。它是JavaMailSender接口的实现类,通过它的send()方法来发送不同类型的邮件,主要分为两类,

一类是简单的文本邮件,不带任何html格式,不带附件,不带图片等简单邮件,

还有一类则是带有html格式文本或者链接,有附件或者图片的复杂邮件。

163邮箱

spring:
	mail:
    		host: smtp.163.com
    		port:
    		username: yimcarson@163.com
    		password: ************

 其中spring.mail.port不指定;spring.mail.password不是邮箱密码,需要登录mail.163.com,前往设置 客户端授权密码中获取的一个16个字符的密码,同时允许POP3/SMTP服务。

 

QQ邮箱

spring:
    mail:
            host: smtp.qq.com
            port: 587
            username: yimcarson@qq.com
            password: ************

spring.mail.password不是QQ密码,登录mail.qq.com,前往设置 账户 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务开启POP3/SMTP服务获取一个16个字符的密码

Gmail邮箱

spring:
    mail: 
        host: smtp.gmail.com
        port: 465
        username: yimcarson@gmail.com
        password: ****************

spring.mail.password是Gmail的密码,但是要前往Google账户的安全性较低的应用的访问权限中允许不安全应用。

Email配置类

用于解析Spring Boot配置文件中mail有关的自定义配置属性 

注:不需要自定义配置的可以不写Email配置类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
 
@Component
public class EmailConfig {
 
	/**
	 * 发件邮箱
	 */
	@Value("${spring.mail.username}")
	private String emailFrom;
 
	public String getEmailFrom() {
		return emailFrom;
	}
 
	public void setEmailFrom(String emailFrom) {
		this.emailFrom = emailFrom;
	}
	
}

或者

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "spring.mail")
public class EmailConfig {
 
	/**
	 * 发件邮箱
	 */
	private String emailFrom;
 
	public String getEmailFrom() {
		return emailFrom;
	}
 
	public void setEmailFrom(String emailFrom) {
		this.emailFrom = emailFrom;
	}
	
}

简单邮件

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleMailTest {
 
	@Autowired
	private MailService mailService;
 
	@Test
	public void sendMail(){
 
		mailService.sendSimpleMail("测试Springboot发送邮件", "发送邮件...");
	}
}
@Override
public void sendSimpleMail(String subject, String text) {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setFrom(mailProperties.getFrom());
    mailMessage.setTo(mailProperties.getTo());
 
    mailMessage.setSubject(subject);
    mailMessage.setText(text);
 
    javaMailSender.send(mailMessage);
}

 结果

带附件的简单邮件

一个文件file.txt,放在resources/public/目录下

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MimeMailTest {
 
	@Autowired
	private MailService mailService;
 
	@Test
	public void testMail() throws MessagingException {
 
		Map<String, String> attachmentMap = new HashMap<>();
		attachmentMap.put("附件", "file.txt的绝对路径");
 
		mailService.sendHtmlMail("测试Springboot发送带附件的邮件", "欢迎进入<a href=\"http://www.baidu.com\">百度首页</a>", attachmentMap);
 
	}
}
@Override
public void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException {
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
 
    //是否发送的邮件是富文本(附件,图片,html等)
    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
 
    messageHelper.setFrom(mailProperties.getFrom());
    messageHelper.setTo(mailProperties.getTo());
 
    messageHelper.setSubject(subject);
    messageHelper.setText(text, true);//重点,默认为false,显示原始html代码,无效果
 
    if(attachmentMap != null){
        attachmentMap.entrySet().stream().forEach(entrySet -> {
            try {
                File file = new File(entrySet.getValue());
                if(file.exists()){
                    messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));
                }
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });
    }
 
    javaMailSender.send(mimeMessage);
}

 结果: 

 

模板邮件

模版引擎

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MailTemplateTest {
 
	@Autowired
	private MailService mailService;
 
	@Test
	public void testFreemarkerMail() throws MessagingException, IOException, TemplateException {
 
		Map<String, Object> params = new HashMap<>();
		params.put("username", "Cay");
 
		mailService.sendTemplateMail("测试Springboot发送模版邮件", params);
 
	}
}

FreeMarker模板

<html>
<body>
	<h3>你好, <span style="color: red;">${username}</span>, 这是一封模板邮件!</h3>
</body>
</html>
@Override
public void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException {
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
 
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
 
    helper.setFrom(mailProperties.getFrom());
    helper.setTo(mailProperties.getTo());
 
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
    configuration.setClassForTemplateLoading(this.getClass(), "/templates");
 
    String html = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
 
    helper.setSubject(subject);
    helper.setText(html, true);//重点,默认为false,显示原始html代码,无效果
 
    javaMailSender.send(mimeMessage);
}

结果: 

 

或者

模版引擎: 

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-velocity</artifactId>
    </dependency>

或者 

    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>

Velocity模板

<tr>
    <td style="padding: 70px 0 0 185px">
        <span style="font-size:14px">Hi,$name:</span>
    </td>
</tr>


<tr>
    <td style="padding: 12px 0 70px 185px">
        <span style="font-size:14px">$tel</span>
    </td>
</tr>
public void sendTemplateMail(String sendTo, String titel, Map<String, Object> content, List<Pair<String, File>> attachments) {
 
		MimeMessage mimeMessage = mailSender.createMimeMessage();
 
		try {
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(emailConfig.getEmailFrom());
			helper.setTo(sendTo);
			helper.setSubject(titel);
 
			String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "template.vm", "UTF-8", content);
			helper.setText(text, true);
			
			for (Pair<String, File> pair : attachments) {
				helper.addAttachment(pair.getLeft(), new FileSystemResource(pair.getRight()));
			}
		} catch (Exception e) {
			throw new RuntimeServiceException(e);
		}
 
		mailSender.send(mimeMessage);
	}

或者

模版引擎

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

Thymeleaf模板

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="'尊敬的 ' + ${user} + '用户:'"></p>
 
<p th:text=" '恭喜您注册成为'+${web}+'网的用户,同时感谢您对'+${company}+'的关注与支持并欢迎您使用'+${product}+'的产品与服务。'"></p>
 
</body>
</html>
public String sendTemplateMail() {
        //创建邮件正文
        Context context = new Context();
        context.setVariable("user", "1111");
        context.setVariable("web", "tttt");
        context.setVariable("company", "我是一个公司");
        context.setVariable("product","我是一个产品");
        String emailContent = templateEngine.process("emailTemplate", context);
        mailService.sendHtmlMail("xxxx@qq.com","主题:这是模板邮件",emailContent);
        return "success";
    }

Service

EmailService 

package club.zstuca.myzstu.service;

import javafx.util.Pair;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Author ShenTuZhiGang
 * @Version 1.0.0
 * @Date 2020-02-15 21:29
 */
public interface EmailService {
    /**
     * 发送简单邮件
     * @param to 收件人地址
     * @param subject  邮件标题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content);

    /**
     * 发送简单邮件
     * @param to 收件人地址
     * @param subject  邮件标题
     * @param content 邮件内容
     * @param attachments<文件名,附件> 附件列表
     */
    public void sendAttachmentsMail(String to, String subject, String content, List<Pair<String, File>> attachments);

    /**
     * 发送模板邮件
     * @param to 收件人地址
     * @param subject  邮件标题
     * @param content<key, 内容> 邮件内容
     * @param attachments<文件名,附件> 附件列表
     */
    public void sendTemplateMail(String to, String subject, Map<String, Object> content, List<Pair<String, File>> attachments);
}

EmailServiceImpl 

package club.zstuca.myzstu.service.Impl;

import club.zstuca.myzstu.config.EmailConfig;
import club.zstuca.myzstu.service.EmailService;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Author ShenTuZhiGang
 * @Version 1.0.0
 * @Date 2020-02-15 21:31
 */
@Service
public class EmailServiceImpl  implements EmailService {
    @Autowired
    private EmailConfig emailConfig;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 用来发送模版邮件
     */
    @Autowired
    private TemplateEngine templateEngine;


    @Override
    public void sendSimpleMail(String to, String subject, String  content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailFrom());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, List<Pair<String, File>> attachments) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailConfig.getEmailFrom());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            if(attachments != null){
                for(Pair<String,File> attachment:attachments){
                    FileSystemResource file = new FileSystemResource(attachment.getValue());
                    helper.addAttachment(attachment.getKey(), file);
                }
            }
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendTemplateMail(String to, String subject, Map<String, Object> content, List<Pair<String, File>> attachments) {
        Context context = new Context();
        context.setVariables(content);
        String emailContent = templateEngine.process("mail", context);
        sendAttachmentsMail(to,subject,emailContent,attachments);
    }
}

 TEST

package club.zstuca.myzstu.email;

import club.zstuca.myzstu.service.EmailService;
import javafx.util.Pair;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ShenTuZhiGang
 * @Version 1.0.0
 * @Date 2020-02-16 11:35
 */
@SpringBootTest
public class EmailTEST {

    @Autowired
    private EmailService emailService;
    @Test
    public void email(){
        emailService.sendSimpleMail("1600337300@qq.com","测试","测试内容");
        List<Pair<String, File>> atta= new ArrayList<>();
        String filename = "E:\\Code\\Project\\JAVA\\myzstu\\src\\main\\resources\\application.properties";
        File file = new File(filename);
        Pair<String, File> pair = new Pair<>("application.properties",file);
        atta.add(pair);
        emailService.sendAttachmentsMail("1600337300@qq.com","测试","测试内容",atta);
        Map<String,Object> content = new HashMap<>();
        content.put("code","abc");
        emailService.sendTemplateMail("1600337300@qq.com","测试",content,atta);
    }
}

常见问题

Gmail邮箱:[Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1]

535 认证失败

邮件发送中出现553问题

Velocity找不到模板文件

163邮箱作为测试服务器,遇到了邮件被认为是垃圾邮件的问题

参考文章

https://blog.csdn.net/a286352250/article/details/53157963

https://blog.csdn.net/caychen/article/details/82887926

https://blog.csdn.net/yimcarson/article/details/84936440

https://blog.csdn.net/shangyuanlang/article/details/80883253

https://blog.csdn.net/lvyuan1234/article/details/80534072

https://blog.csdn.net/YShuaiLong/article/details/83378868

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starzkg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值