java 发送附件邮件给指定邮箱
java SpringBoot 发送附件邮件给指定邮箱
1.导入依赖
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-test</artifactId> </dependency>
2.添加配置
application.yml
3.编写服务应用
4.调用发送接口
@PostMapping("/email") public Result contextLoads(@RequestBody Map<String, Object> param, HttpServletRequest request,HttpServletResponse response) { String email = (String) param.get("email"); Integer id = (Integer) param.get("id");//文章id HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); if(user==null){ return Result.fail("请登录!"); } String name = user.getName(); Content byId = contentService.getById(id); response.setCharacterEncoding("utf-8"); //设置响应的内容类型 response.setContentType("text/markdown"); boolean b = userService.sendAttachmentMail(name, byId,email); if (b){ return Result.ok(); } return Result.fail(); }
5.成功响应
{ "code": 200, "message": "成功", "data": null, "ok": true }