关键词:
虽然现在短信验证已经最流行也是最常用的验证方式;但是邮件验证还是必不可少,依然是网站的必备功能之一。什么注册验证,忘记密码或者是给用户发送营销信息都是可以使用邮件发送功能的。最早期使用JavaMail的相关api来进行发送邮件的功能开发,后来spring整合了JavaMail的相关api推出了JavaMailSender更加简化了邮件发送的代码编写,现在springboot对此进行了封装就有了现在的spring-boot-starter-mail。
1、 新建项目sc-mail,对应的pom.xml文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-cloud</groupId> <artifactId>sc-mail</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-mail</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2、 新建配置文件application.yml
spring: application: name: sc-mail mail: host: smtp.qq.com #邮箱服务器地址 port: 465 username: 515768476@qq.com #用户名 password: vfcqhwsnnwugbhcx #密码 (改成自己的密码) default-encoding: UTF-8 properties: mail: smtp: ssl: enable: true
3、 新建邮件发送服务类
package sc.mail.service.impl; import java.io.File; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 sc.mail.service.MailService; @Service public class MailServiceImpl implements MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; /** * 文本 * @param from * @param to * @param subject * @param content */ @Override public void sendSimpleMail(String from, String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { mailSender.send(message); logger.info("simple mail had send。"); } catch (Exception e) { logger.error("send mail error", e); } } /** * @param from * @param to * @param subject * @param content */ public void sendTemplateMail(String from, String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); mailSender.send(message); logger.info("send template success"); } catch (Exception e) { logger.error("send template eror", e); } } /** * 附件 * * @param from * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String from, String to, String subject, String content, String filePath){ MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); mailSender.send(message); logger.info("send mail with attach success。"); } catch (Exception e) { logger.error("send mail with attach success", e); } } /** * 发送内嵌图片 * * @param from * @param to * @param subject * @param content * @param imgPath * @param imgId */ public void sendInlineResourceMail(String from, String to, String subject, String content, String imgPath, String imgId){ MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(imgId, res); mailSender.send(message); logger.info("send inner resources success。"); } catch (Exception e) { logger.error("send inner resources fail", e); } } }
4、 新建测试类
package sc.mail; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import sc.mail.service.MailService; @RunWith(SpringRunner.class) @SpringBootTest public class MailSendTest { @Autowired private MailService mailService; @Test public void sendSimpleMailTest() { mailService.sendSimpleMail("515768476@qq.com", "happy.huangjinjin@163.com", "sendSimpleMailTest", "sendSimpleMailTest from 515768476@qq.com"); } @Test public void sendTemplateMailTest() { String html = "<html><body>" + " <div> " + " sendTemplateMailTest from 515768476@qq.com </br>" + " <b>这是模板邮件</b>" + "</div>" + "</body></html>"; mailService.sendTemplateMail("515768476@qq.com", "happy.huangjinjin@163.com", "sendTemplateMailTest", html); } @Test public void sendAttachmentsMailTest() { String filePath = "D:\\springcloudws\\sc-mail\\src\\main\\java\\sc\\mail\\service\\impl\\MailServiceImpl.java"; mailService.sendAttachmentsMail("515768476@qq.com", "happy.huangjinjin@163.com", "sendAttachmentsMailTest", "sendAttachmentsMailTest from 515768476@qq.com", filePath); } @Test public void sendInlineResourceMailTest() { String imgId = "img1"; String content = "<html><body>" + "sendInlineResourceMailTest:<img src=\'cid:" + imgId + "\' >" + "</body></html>"; String imgPath = "D:\\springcloudws\\sc-mail\\src\\main\\resources\\20181015223228.jpg"; mailService.sendInlineResourceMail("515768476@qq.com", "happy.huangjinjin@163.com", "sendAttachmentsMailTest", content, imgPath, imgId); } }
5、 运行测试类验证是否发送邮件成功
简单邮件
模板邮件
附件邮件
内嵌图片邮件
springboot发送邮件
源码:https://gitee.com/smfx1314/sendMailSpringBoot中发送邮件步骤SpringBoot中发送邮件具体的使用步骤如下1、添加Starter模块依赖2、添加SpringBoot配置(QQ/网易系/Gmail)3、调用JavaMailSender接口发送邮件开始编码创建springboot项目,添加依赖项目... 查看详情
springboot实现发送简单邮件(代码片段)
文章目录SpringBoot实现发送简单邮件0、发送邮件的简单原理介绍1、开启我们发送方邮件的STMP服务2、加入Mail依赖,使得SpringBoot项目支持邮件服务3、配置信息,连接邮箱服务器以及STMP服务4、使用SpringBoot提供的对象使用邮件服... 查看详情
springboot高级特性之邮件发送(代码片段)
...件发送的场景如发送验证码,向客户发送邮件等等。springboot中整合了mail帮助我们更方便的发送邮件平时我们发送邮件是通过邮件的服务器发送出去的比如qq邮件调用qq的邮件服务器网易的邮件通过网易的服务器我们使用邮件... 查看详情
springboot实战一:发送邮件
...件,HTML邮件,附件邮件,图片邮件模板邮件来进行一个SpringBoot项目的实战,发送一下邮件,这里我们先了解一下邮件的协议邮件协议SMTP协议:简单邮件传输协议(SimpleMailTransferProtocol),邮件从一台服务器传送到另一台服务器POP3... 查看详情
补习系列(12)-springboot与邮件发送
目录一、邮件协议关于数据传输二、SpringBoot与邮件A.添加依赖B.配置文件C.发送文本邮件D.发送附件E.发送Html邮件三、CID与图片参考文档一、邮件协议在谈谈代码之前,先来了解下邮件的基本协议。电子邮件协议是基于TCP层定义的... 查看详情
springboot使用javamailsender发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送。在SpringBoot中也提供了相应的自动化配置。这篇文章主要讲如何在SpringBoot中使用JavaMailSender发送邮件。 发送邮件1,在pom.xml中引入spring-boot-starter-mail依赖: <dependency>... 查看详情
springboot整合邮件发送
在做项目的过程中,难免会遇到要发送邮件的情况。这里,将springboot与邮件发送整合一下:一:添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependen 查看详情
springboot发送邮件
springboot发送邮件1.依赖文件pom.xml:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>2.配置文件:#####163邮箱## 查看详情
springboot开篇简单邮件发送
上篇终结篇为spring发送邮件,这次将使用springboot发送邮件,同时本篇将作为springboot入门篇。新建一个工程。。工程目录结构如下,此次使用idea进行开发。对于一个长期使用eclipse的人来说,真的是很不习惯。但是发现idea比E... 查看详情
springboot中使用javamailsender发送邮件
...Spring提供了非常好用的JavaMailSender接口实现邮件发送。在SpringBoot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在SpringBoot中使用JavaMailSender发送邮件。快速入门在SpringBoot的工程中的pom.xml中引入spring-boot-starter-mail... 查看详情
springboot发送邮件,端口号大有玄机
...件3.3.4使用Freemarker作邮件模板3.3.5使用Thymeleaf作邮件模板SpringBoot发送邮件,松哥之前专门写过文章,这里就不啰嗦了。 查看详情
使用springboot配置发送邮件功能(代码片段)
1、使用SpringBoot配置发送邮件功能项目总体结构用户表设计SETFOREIGN_KEY_CHECKS=0;CREATEDATABASEsample;USEsample;setnamesutf8;--------------------------------Tablestructurefortab_mail------------------------------DROPTABLEIFE 查看详情
springboot发送简单邮件(代码片段)
使用SpringBoot发送简单邮件1.在pom.xml中导入依赖<!--邮件依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency& 查看详情
springboot发送邮件
参考技术A源码:https://gitee.com/smfx1314/sendMailSpringBoot中发送邮件具体的使用步骤如下创建springboot项目,添加依赖项目结构1、添加依赖在Mavenpom.xml配置文件中加入spring-boot-starter-mail依赖。2、添加配置参数然后在application.yml文件中... 查看详情
springboot发送邮件
引入maven<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency> yml配置spring:mail:host:smtp.163.com#邮件服 查看详情
springboot实现发送简单邮件(代码片段)
文章目录SpringBoot实现发送简单邮件0、发送邮件的简单原理介绍1、开启我们发送方邮件的STMP服务2、加入Mail依赖,使得SpringBoot项目支持邮件服务3、配置信息,连接邮箱服务器以及STMP服务4、使用SpringBoot提供的对象使用邮件服... 查看详情
springboot实现发送邮件
1.QQ邮箱发送邮件设置首先登录QQ邮箱>>>登录成功后找到设置>>>然后找到邮箱设置>>>点击账户>>>找到POP3|SMTP服务>>>点击开启(开启需要验证,验证成功后会有一串授权码用于发送邮件使用)>>>... 查看详情
springboot用163邮箱、阿里云企业邮箱发送邮件的配置
参考技术Aspringboot+JavaMail用163和阿里云企业邮箱的smtp服务都可以发送邮件,区别在于发送人的邮箱地址。用163免费邮箱,收件人看到的发件邮箱是XXX@163.com,用阿里云企业邮箱可以用自己的域名,例如account@mydomain.com。当然,也... 查看详情