一、获取邮箱授权码

1、以163邮箱为例,页面首部找到设置,选择SMTP

2、开启POP3/SMTP服务

3、获取授权码

二、SpringBoot集成邮件发送

1、环境配置
  • 添加依赖
<!-- springboot 邮件mail --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>${springboot.version}</version></dependency>
  • 配置文件
# 发送邮件配置
spring.mail.protocol=smtps
# 配置 smtp 服务器地址
spring.mail.host=smtp.163.com
#服务器的端口
spring.mail.port=465
# 配置邮箱用户名
spring.mail.username=xxx@163.com
# 配置申请到的授权码(刚让复制的授权码)
spring.mail.password=xxx
# 配置邮件编码
spring.mail.default-encoding=UTF-8
# 配饰 SSL 加密工厂
spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
# 发送方邮件
spring.mail.from=xxx@163.com
邮箱类型 SMTP服务器地址 端口号
QQ邮箱 smtp.qq.com 465或587
sina邮箱 smtp.sina.cn 465或587
126邮箱 smtp.126.com 465或994
aliyun邮箱 smtp.aliyun.com 465或994
163邮箱 smtp.163.com 465或994
yeah邮箱 smtp.yeah.net 465或994
开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误
  debug: true

三、代码编写

  • 工具类
@Component
public class SendEmailUtil {@Value("${spring.mail.from}")private String from; // 发送发邮箱地址@Autowiredprivate JavaMailSender mailSender;/*** 发送纯文本邮件信息** @param to      接收方* @param subject 邮件主题* @param content 邮件内容(发送内容)*/public void sendMessage(String to, String subject, String content) {// 创建一个邮件对象SimpleMailMessage msg = new SimpleMailMessage();msg.setFrom(from); // 设置发送发msg.setTo(to); // 设置接收方msg.setSubject(subject); // 设置邮件主题msg.setText(content); // 设置邮件内容// 发送邮件mailSender.send(msg);}/*** 发送带附件的邮件信息** @param to      接收方* @param subject 邮件主题* @param content 邮件内容(发送内容)* @param files 文件数组 // 可发送多个附件*/public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {MimeMessage mimeMessage = mailSender.createMimeMessage();//解决附件文件名称过长乱码问题System.setProperty("mail.mime.splitlongparameters","false");try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);helper.setFrom(from); // 设置发送发helper.setTo(to); // 设置接收方helper.setSubject(subject); // 设置邮件主题helper.setText(content,true); // 设置邮件内容if (files != null && files.length > 0) { // 添加附件(多个)for (File file : files) {helper.addAttachment(MimeUtility.encodeWord(file.getName(), "utf-8", "B"), file);}}} catch (MessagingException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 发送邮件mailSender.send(mimeMessage);}/*** 发送带附件的邮件信息** @param to      接收方* @param subject 邮件主题* @param content 邮件内容(发送内容)* @param file 单个文件*/public void sendMessageCarryFile(String to, String subject, String content, File file) {MimeMessage mimeMessage = mailSender.createMimeMessage();//解决附件文件名称过长乱码问题System.setProperty("mail.mime.splitlongparameters","false");try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);helper.setFrom(from); // 设置发送发helper.setTo(to); // 设置接收方helper.setSubject(subject); // 设置邮件主题helper.setText(content,true); // 设置邮件内容helper.addAttachment(MimeUtility.encodeWord(file.getName(), "utf-8", "B"), file); // 单个附件} catch (MessagingException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 发送邮件mailSender.send(mimeMessage);}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}
}

四、测试邮件发送

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AmqpApplication.class)
public class ApiApplicationTests {@Resourceprivate SendEmailUtil emailUtil;@Testpublic void sendStringEmail() {// 测试文本邮件发送(无附件)String to = "axxx@163.com"; // 接收邮箱地址String title = "文本邮件发送测试";String content = "文本邮件发送测试";emailUtil.sendMessage(to, title, content);}@Testpublic void sendFileEmail() {// 测试单个附件邮件发送String to = "axxx@163.com"; // 接收邮箱地址String title = "温度信息附件邮件发送测试";String content = "温度信息附件邮件发送测试";File file = new File("C:\\Users\\xsj\\Desktop\\问题.docx");emailUtil.sendMessageCarryFile(to, title, content, file);}@Testpublic void sendFilesEmail() {// 测试多个附件邮件发送String to = "axxx@163.com"; // 接收邮箱地址String title = "多个温度信息附件邮件发送测试";String content = "多个温度信息附件邮件发送测试";File[] files = new File[2];files[0] = new File("C:\\Users\\xsj\\Desktop\\位图\\微信_20220729142127.jpg");files[1] = new File("C:\\Users\\xsj\\Desktop\\问题.docx");emailUtil.sendMessageCarryFiles(to, title, content, files);}}

springboot发送邮件-163邮箱相关推荐

  1. SpringBoot 整合163邮箱 阿里云25端口问题

    SpringBoot 整合163邮箱阿里云25端口问题 使用 163 邮箱 SMTP服务器 非SSL协议 25端口,项目本地测试时没有问题, 但是发布到阿里云服务器就报错 经检查发现阿里云出于安全考虑 ...

  2. Java邮箱发送邮件(163邮箱)

    一.注册163邮箱 163网易免费邮--中文邮箱第一品牌 二.打开邮箱服务获取授权码 1.进入主页.找到服务开启页 2.点击开启服务 3.使用注册手机号,发送验证码,即可获得授权码 三.编写代码(需要 ...

  3. 调用API发送邮件163邮箱Python

    #发邮件的库 import smtplib# from email.mime.text import MIMEText #SMTP服务器 SMTPSever = "smtp.163.com& ...

  4. SpringBoot发送邮件(网易邮箱,QQ邮箱,139邮箱)

    添加依赖 <!-- 邮件服务 --> <dependency><groupId>org.springframework.boot</groupId>&l ...

  5. SpringBoot发送邮件(QQ邮箱、腾讯企业邮箱、网易邮箱、阿里云邮箱...)

    目录 一.获取邮箱授权码 二.引入依赖 三.配置文件(请注意yml格式,或者使用.properties) 四.不带附件发邮箱 五.带附件发邮箱 六.邮箱类型拓展 本篇文章以QQ邮箱作为发件人写案例演示 ...

  6. springboot 发送邮件 QQ邮箱 535error

    授权码什么的问题我就不说了,因为那个属于配置问题. 这个问题主要出现在我想要多次连接然后测试的情况下,才会报535error Caused by: javax.mail.AuthenticationF ...

  7. SpringBoot集成163邮件发送详细配置,从163邮箱开始配置

    SpringBoot集成163邮件发送详细配置,从163邮箱开始配置 1.登录163邮箱 2.配置163邮箱 3.开始编写SpringBoot代码 1.创建SpringBoot项目然后引入依赖 2.编 ...

  8. spring-boot邮件发送功能演示(163邮箱与QQ邮箱互发)

    背景:使用 Spring Boot 发送邮件,演示的邮箱为 163邮箱与QQ邮箱,两者可以实现互发邮件. 问题:自己可以实现一下 QQ邮箱 发 QQ邮箱,163发163,是什么样的. 演示 :163 ...

  9. app里使用163邮箱发送邮件,被163认为是垃圾邮件的坑爹经历!_ !

    最近有个项目,要发邮件给用户设定的邮箱报警,然后就用了163邮箱,代码是网上借来的^^,如下: package com.smartdoorbell.util;import android.os.Asy ...

最新文章

  1. zabbix 安装时的报错mysql_connect(): Access denied for us
  2. 使用ansible部署安装corosync+pacemaker
  3. hihoCoder week3 KMP算法
  4. Linux 下查看文件的命令介绍
  5. Odoo免费开源企业信息化平台助力企业成功
  6. 一文读懂 Linux 内存分配全过程
  7. Python 库的使用 —— dis
  8. AC自动机(HDU 2222: Keywords Search)
  9. Java之JDBC安装、使用详解(2021最新!)
  10. 树莓派PICO使用MicroPython + HX1838 接收遥控器数据 NEC解码
  11. 变量的存在意义和底层逻辑(《Java与Python学习通法》)
  12. java 输出小写‘a‘-‘z‘和大写‘A‘-‘Z‘
  13. 炸机不可怕,可怕的是你不知道为什么炸
  14. 管程模型解决并发编程
  15. 使用正则表达式批量去除第一个逗号前的内容(含逗号)以及去除最后一个逗号后面的内容
  16. 基于Python的直播平台数据分析可视化系统
  17. 论文阅读《Dense Relation Distillation with Context-aware Aggregation for Few-Shot Object Detection》
  18. 2022.05.18古月珩A股复盘,数据掘金,主题发现(全网同名)
  19. 口腔健康核心信息及知识要点
  20. 《李小龙传奇》观后感

热门文章

  1. 渗透测试之AppScan篇
  2. 使用distpicker的简单测试页面
  3. EPI晶片的表面微观粗糙度对湿化学处理的依赖性
  4. 模拟htonl、ntohl、htons、ntohs函数实现
  5. 第2节--深度学习基础介绍-机器学习--课程介绍(下)
  6. 什么是龙格现象(Runge phenomenon)?如何避免龙格现象?
  7. python超市收银程序_用java编写超市收银小程序
  8. AtCoder Regular Contest 105 C - Camels and Bridge
  9. 服务器基础设置:H3C服务器,ilo地址,管理员密码、PXE启动、虚拟化是否打开
  10. Python编程基础(1)