赞
踩
发送短信,会生成一行字符。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
#邮箱类型
spring.mail.host=smtp.qq.com
#邮箱名字
spring.mail.username=codel115@qq.com
#邮箱的密匙 【需要去相关的邮箱里面申请】
spring.mail.password=申请是密匙
spring.mail.default-encoding=UTF-8
#自定义的数据 【邮件发送人】
mail.from=codel11158@qq.com
public interface MailService {
void sendSimpleMail(String mail);
}
package com.codel.mail.sersvice.impl;
import com.codel.mail.sersvice.MailService;
import lombok.extern.slf4j.Slf4j;
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;
/**
* 邮件服务impl
* 需要先构建 SimpleMailMessage或 MimeMessage邮件信息类来填写邮件标题、邮件内容等信息,
* 最后提交给JavaMailSenderImpl发送邮件,
*
* @author codel
* @date 2021/09/23
*/
@Service
@Slf4j
public class MailServiceImpl implements MailService {
// JavaMailSender 实现 MailSender 拥有发送文件的功能
@Autowired
private JavaMailSender mailSender;
@Value("${mail.from}")
private String mailFrom;
/**
* 发送简单的邮件
* mailFrom 是发件人,
* mailTo 是收件人。
* message.setSubject()设置邮件主题。
* message.setText()设置邮件内容。
* mailSender.send(message)是发送短信。
* @param mailTo 邮件
*/
@Override
public void sendSimpleMail(String mailTo) {
//创建邮件 SimpleMailMessage是创建文件 包括内容,邮件消息,发件人,收件人……
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailFrom);
message.setTo(mailTo);
message.setSubject("simple mail");
message.setText("hello world");
//发送邮件
mailSender.send(message);
log.info("邮件已经发送");
}
}
Spring 自带了一个 MailSender 的实现 JavaMailSenderImpl,它会使用 JavaMail API 来发送 Email。Spring 或 SpringBoot 应用在发送 Email 之前,我们必须要JavaMailSenderImpl 装配为 Spring应用上下文的一个 bean。
package com.codel.mail.controller;
import com.codel.mail.sersvice.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 邮件控制器
*
* @author codel
* @date 2021/09/23
*/
@RestController
@RequestMapping("/mail")
public class MailController {
@Autowired
private MailService mailService;
/**
* 发送邮件
*
* @param userName 用户名
* 把接收人的信息传递进去
* @return {@link String}
*/
@GetMapping("/send")
public String sendMail(@RequestParam(value = "userName") String userName) {
mailService.sendSimpleMail(userName);
return "success";
}
}
上面是邮件的文字发送,
public interface MailService {
void sendSimpleMail(String mail);
void sendMail(String mail);
}
service类
/**
* 发送附件邮件
* MimeMessage 比 SimpleMailMessage 功能更强大,可以发送附件,也可以将内容转成html 格式发送。
* 所以一般实际使用的时候都使用MimeMessage。
* 另外发送附件,还需要借助MimeMessageHelper 。
* MimeMessageHelper是辅助MimeMessage的。
*
* @param mail 邮件
*/
@Override
public void sendMail(String mail) {
//
MimeMessage message=mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(mail);
helper.setSubject("simple mail");
helper.setText("hello world", true);
FileSystemResource file = new FileSystemResource(new File("D:\\大四\\1.png"));
String fileName = file.getFilename();
//判断是否为空
assert fileName != null;
// helper.addAttachment()是添加附件的。
helper.addAttachment(fileName, file);
mailSender.send(message);
log.info("附件邮件已经发送");
} catch ( MessagingException e) {
log.error("{}",e);
}
}
controller类
/**
* 发送附件电子邮件
*
* @param username 用户名
* @return {@link String}
*/
@GetMapping("/sendEmail/{username}")
public String sendEmail(@PathVariable("username") String username){
mailService.sendMail(username);
return "附件文件发送成功";
}
运行结果:
导入模板引擎依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置模板页面
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户验证码</title>
</head>
<body>
<div><span th:text="${username}">XXX</span> 先生/女士,您好: </div>
<p style="text-indent: 2em">您的用户验证码为<span th:text="${code}" style="color: cornflowerblue">123456</span>,请妥善保管。></p>
</body>
</html>
username和code两个动态变量都是和controller后台绑定的。
定制邮件发送服务
/**
* 发送电子邮件模板
*
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendTemplateEmail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//使用MimeMessageHelper帮助类并设置multpart多部件使用为true
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//发送邮件
mailSender.send(message);
System.out.println("模板邮件发送成功!");
} catch (MessagingException e) {
System.out.println("模板邮件发送失败");
e.printStackTrace();
}
}
这里的收件人可以使用一个数组来接收,就是说可以多人接收同一个邮件。
模拟测试传递信息
/**
* 测试发送模板邮件
*/
@Autowired
private MailServiceImpl mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void sendTemplateEmail() {
String to = "codel1115@qq.com";
String subject = "【模板邮件】标题";
//使用模板定制邮件正文内容
Context context = new Context();
context.setVariable("username", "codeL");
context.setVariable("code", "489");
//使用TemplateEngline设置要处理的模板 页面
String emailTemplate = templateEngine.process("test", context);
//发送模板邮件
mailService.sendTemplateEmail(to, subject, emailTemplate);
}
测试结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。