赞
踩
SpringBoot实现邮件功能是非常的方便快捷的,因为SpringBoot默认有starter实现了Mail。最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring退出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了
现在的spring-boot-starter-mail。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
QQ邮箱->设置->账户->POP3/SMTP服务:先启动服务,再获得生成的授权码.
application.yml的配置信息
spring: mail: #smtp服务主机 qq邮箱则为smtp.163.com host: smtp.qq.com #服务协议 protocol: smtp # 编码集 default-encoding: UTF-8 #发送邮件的账户 username: xxx@qq.com #上一小节获取的授权码 password: test-connection: true properties: mail: smtp: auth: true starttls: enable: true required: true
工具类R:
package com.sakura.mail.common; import lombok.Data; import java.util.HashMap; import java.util.Map; /** * 通用返回结果,服务端响应的数据最终都会封装成此对象 * @param <T> */ @Data public class R<T> { private Integer code; //编码:1成功,0和其它数字为失败 private String msg; //错误信息 private T data; //数据 private Map map = new HashMap(); //动态数据 public static <T> com.sakura.mail.common.R<T> success(T object) { com.sakura.mail.common.R<T> r = new com.sakura.mail.common.R<T>(); r.data = object; r.code = 1; return r; } public static <T> com.sakura.mail.common.R<T> error(String msg) { com.sakura.mail.common.R r = new com.sakura.mail.common.R(); r.msg = msg; r.code = 0; return r; } public com.sakura.mail.common.R<T> add(String key, Object value) { this.map.put(key, value); return this; } }
package com.sakura.mail.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * Mail实体类 */ @Data @NoArgsConstructor @AllArgsConstructor public class Mail implements Serializable { /** * 邮件接收方,可多人 */ private String[] tos; /** * 邮件主题 */ private String subject; /** * 邮件内容 */ private String content; }
package com.sakura.mail.controller; import com.sakura.mail.bean.Mail; import com.sakura.mail.common.R; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Serendipity * @ClassName MailController.java * @Description TODO * @createTime 2022-11-24- 10:56:00 */ @RestController @RequestMapping("/mail") @Slf4j public class MailController { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; /** * 发送邮件 * @param mail * @return */ @PostMapping("/send") public R<Mail> commonEmail(Mail mail) { //创建简单邮件消息 SimpleMailMessage message = new SimpleMailMessage(); //谁发的 message.setFrom(from); //谁要接收 message.setTo(mail.getTos()); //邮件标题 message.setSubject(mail.getSubject()); //邮件内容 message.setText(mail.getContent()); try { mailSender.send(message); return R.success(mail); } catch (MailException e) { log.error(e.getMessage()); return R.error("发送邮件失败!"); } } }
直接使用Postman进行测试:
return R.success(mail);
} catch (MailException e) {
log.error(e.getMessage());
return R.error("发送邮件失败!");
}
}
}
# 3. 测试
直接使用Postman进行测试:
![在这里插入图片描述](https://img-blog.csdnimg.cn/3188f13d5b974888972667b49538f18b.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。