写作原因:

项目接近尾声,需求一变再变,其实技术点从未改变,只是业务逻辑的变更,发送邮件提醒的功能,两个月变更七次。我想把技术点记录下来,这里无关乎业务,只有发送邮件的功能。

邮件发送准备说明:

由于公司项目需求,所以我们使用的邮箱是本公司内部邮箱,所以部门给我们系统提供的邮箱是国际邮箱,也就是最高权限邮箱。所以邮箱配置,需要根据提供邮箱的形式配置。

pom.xml

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

application.yml

spring:
mail:
host: smtp.xxx.com
port: 25
username: xxx@xxx.com
password: xxx
default-encoding: UTF-8
thymeleaf: false

注意:host:属性默认是 JavaMail 会话的主机;

port:端口默认监听标准的 SMTP 端口25,如果公司内部禁用的话,可以采用465;

username:发送方的邮箱;

password:邮箱密码,如果是qq或者163邮箱的话,是邮箱授权码;

default-encoding:邮件编码字符集;

thymeleaf:模板生成email,Spring 给出的解决方案是 使用模板生成Email,有多种模板方案可供选择;

controller :

import io.swagger.annotations.Api;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mail")
@Api(description = "邮件发送")
public class MailSendController {

@Autowired
private IMailService service;

@PostMapping("/sendText")
public ResponseStatus send() {
return service.sendMail();
}
}

service:

import xxx.xxx.xxx.xxx.ResponseStatus;


public interface IMailService {

public void sendMail();
}

IMailServiceImpl:

import xxx.xxx.xxx.xxx.IMailService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.Date; @Service
public class IMailServiceImpl implements IMailService { @Resource
private JavaMailSender javaMailSender; @Value("${spring.mail.username}")
private String from; @Override
public void sendMail(String title, String text, String to) {
//邮件对象
SimpleMailMessage message = new SimpleMailMessage(); //邮件主题
String title = "我是测试邮件的主题";
//邮件内容
String text = "我是这封测试邮件的内容,内容多多,可以根据自身业务填充";
//接收邮件人邮箱
String to = "xxx@xxx"; // 发送人的邮箱(系统配置的邮箱)
message.setFrom(from);
//发给谁对方邮箱(接收有邮件人邮箱)
message.setTo(to);
//标题
message.setSubject(title);
//内容
message.setText(text);
//发件日期
message.setSentDate(new Date());
//发送
javaMailSender.send(message);
ResponseStatus.ok();
}
}

总结:

1、yum文件中邮箱配置需要特别注意,不同邮箱在配置password的时候,应特别注意,企业自己邮箱未经设置的话,是邮箱密码,我采用的这种形式。QQ邮箱及163邮箱在此处是邮箱授权码,如何取得授权码可自行问“度娘”。

2、邮件端口设置默认25,如果端口被占用的话使用465。

3、SpringBoot中集成邮件服务记录,小白成长中,望不吝赐教。

本文作者:[魂皓轩][1] 欢迎关注公众号

本人保留所有权益,转载请注明出处。

欢迎有故事、有想法的朋友和我分享,可发送至 e-mail: lwqforit@163.com

[1]: 1 "文章编辑专用,同步微信公众号,微信,博客园,知乎,微博,思否(segmentfault),掘金,QQ

springboot 简单邮件发送的更多相关文章

  1. springboot添加邮件发送及压缩功能

    springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...

  2. SpringBoot整合邮件发送

    本节介绍SpringBoot项目如何快速配置和发送邮件,包括简单的邮件配置.发送简单邮件.发送HTML邮件.发送携带附件的邮件等. 示例源码在:https://github.com/laolunsi/ ...

  3. SpringBoot集成邮件发送

    一:简述 在日常中的工作中难免会遇到程序集成邮件发送功能.接收功能:此篇文章我将使用SpringBoot集成邮件发送功能和接收功能:若对邮件一些基本协议和发送流程不懂的请务必参考我之前写的博客或者浏览 ...

  4. 补习系列(12)-springboot 与邮件发送

    目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...

  5. springboot实现邮件发送

    1.创建springboot项目. 2.创建好的项目如图: 在static目录下新建index.html. 3.点击启动项目 在浏览器的地址栏中访问:http://localhost:8080/ 访问 ...

  6. 补习系列(12)-springboot 与邮件发送【华为云技术分享】

    目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...

  7. 使用javaMail实现简单邮件发送

    一.首先你要用来发送邮件的qq邮箱需要开通pop3/smtp服务,这个可以百度一下就知道了 二.导入所需要的jar包,我使用的是maven添加依赖 <dependency> <gro ...

  8. springboot+kafka+邮件发送(最佳实践)

    导读 集成spring-kafka,生产者生产邮件message,消费者负责发送 引入线程池,多线程发送消息 多邮件服务器配置 定时任务生产消息:计划邮件发送 实现过程 导入依赖 <proper ...

  9. springboot开篇 (一)简单邮件发送

    上篇终结篇为spring 发送邮件,这次将使用springboot 发送邮件,同时本篇将作为springboot入门篇. 新建一个工程..工程目录结构如下,此次使用idea进行开发.对于一个长期使用e ...

随机推荐

  1. 【NS2】学习资源

    查找资料时在网上看到不错的资源汇总博客 1 http://blog.csdn.net/chenkai619/article/category/1084323 2 http://blog.sina.co ...

  2. React Native开源项目如何运行(附一波开源项目)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

  3. @总结 - 1@ 多项式乘法 —— FFT

    目录 @0 - 参考资料@ @1 - 一些概念@ @2 - 傅里叶正变换@ @3 - 傅里叶逆变换@ @4 - 迭代实现 FFT@ @5 - 参考代码实现@ @6 - 快速数论变换 NTT@ @7 - ...

  4. deepin golang微服务搭建go-micro环境

    1.安装micro 需要使用GO1.11以上版本 #linux 下 export GO111MODULE=on export GOPROXY=https://goproxy.cn # 使用如下指令安装 ...

  5. SDUT-2054_数据结构实验之链表九:双向链表

    数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 学会了单向链表,我们又多了一种解决问题的 ...

  6. K8s中Pod健康检查源代码分析

    了解k8s中的Liveness和Readiness Liveness: 表明是否容器正在运行.如果liveness探测为fail,则kubelet会kill掉容器,并且会触发restart设置的策略. ...

  7. @bzoj - 3836@ [Poi2014]Tourism

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个n个点,m条边的无向图,其中你在第i个点建立旅游站点的费 ...

  8. vue 后期追回的属性不更新视图问题

    this.$set(this.problemList[index], 'sub', [])   因为原始数组是有set,get而追加的没有,所以需要用这种方式   // 添加 this.$set(th ...

  9. Educational Codeforces Round 5(A,B题)

    虽然是水题但还是贴下代码把 A #include<cstring> #include<cstdio> using namespace std; ; char x[qq],y[q ...

  10. Yarn install 报错 Resolving packages... [2/4] Fetching packages... info There appears to be trouble with your network connection. Retrying

    1.设置淘宝代理 yarn config set registry 'https://registry.npm.taobao.org' 2.如果网址本地可以打开,说明你本地有代理设置 所以需要按本地的 ...