邮件
电子邮件仍然是我们企业间交往的一种非常常见的方式
发送简单邮件
第一步首先导入坐标
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.6.13</version></dependency>
这个依赖使我们去springboot的
版本适配依赖库里去找的
这里面写了规范
很多依赖的坐标
可以直接导入 而不会出现版本冲突的问题
第二步做配置
我们要去邮箱客户端进行配置
在yml文件里配置了用户名 密码 还有供应商
还有传输协议
spring:mail:username: 1732446549@qq.compassword: 123456host: smtp.qq.com
接下来我们要进行具体操作
我们首先做一个核心业务层接口
package com.bigdata1421.mail.service;import org.springframework.stereotype.Service;@Service
public interface SendMailService {void sendMail();}
接下来写业务层接口的实现类
我们这个时候要注入一个JavaMailSender对象
通过JavaMailSender对象发送邮件
package com.bigdata1421.mail.service.impl;import com.bigdata1421.mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;import javax.annotation.Resource;public class sendMailServiceImpl implements SendMailService {@Resourceprivate JavaMailSender javaMailSender;@Overridepublic void sendMail() {SimpleMailMessage message=new SimpleMailMessage();javaMailSender.send(message);}}
我们查看一下页面模型
我们程序中要有四个信息
然后设置
即可
package com.bigdata1421.mail.service.impl;import com.bigdata1421.mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;import javax.annotation.Resource;public class sendMailServiceImpl implements SendMailService {@Resourceprivate JavaMailSender javaMailSender;//发送人private String from="123@qq.com";//接收人private String to="456@qq.com";//标题private String subject="测试文件";//正文private String context="正文内容";@Overridepublic void sendMail() {SimpleMailMessage message=new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(context);javaMailSender.send(message);}}
我们进行一个测试
在测试中注入业务层接口
调用接口方法 sendMail
package com.bigdata1421.mail;import com.bigdata1421.mail.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class MailApplicationTests {@Autowiredprivate SendMailService sendMailService;@Testvoid contextLoads() {sendMailService.sendMail();}}
发送多封邮件
大概是这样
package com.bigdata1421.mail.service.impl;import com.bigdata1421.mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.*;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.annotation.Resource;public class sendMailServiceImpl implements SendMailService {@Resourceprivate JavaMailSender javaMailSender;//发送人private String from="123@qq.com";//接收人private String to="456@qq.com";//标题private String subject="测试文件";//正文private String context="正文内容";@Overridepublic void sendMail() {try {MimeMessage message = javaMailSender.createMimeMessage();MimeMessageHelper helper=new MimeMessageHelper(message);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(context);javaMailSender.send(message);} catch (Exception e) {e.printStackTrace();}}}
如果我们对正文内容进行格式解析html
我们要加入重载参数 true
那我们如何发送附件呢
用File类就行
这就是发送邮件和发送附件
但是这样还不够
我们还是要打开一个开关
就能成功接收
发送附件的核心
在于包装一下应用类
个人号推广
博客主页
多多!-CSDN博客
Web后端开发
https://blog.csdn.net/qq_30500575/category_12624592.html?spm=1001.2014.3001.5482
Web前端开发
https://blog.csdn.net/qq_30500575/category_12642989.html?spm=1001.2014.3001.5482
数据库开发
https://blog.csdn.net/qq_30500575/category_12651993.html?spm=1001.2014.3001.5482
项目实战
https://blog.csdn.net/qq_30500575/category_12699801.html?spm=1001.2014.3001.5482
算法与数据结构
https://blog.csdn.net/qq_30500575/category_12630954.html?spm=1001.2014.3001.5482
计算机基础
https://blog.csdn.net/qq_30500575/category_12701605.html?spm=1001.2014.3001.5482
回忆录
https://blog.csdn.net/qq_30500575/category_12620276.html?spm=1001.2014.3001.5482