/** * <p>Title: EmailController.java</p> * <p>Description: </p> * @author dsc * @date 2020年5月25日 * @version 1.0 */ package cn.timer.api.controller.email; import java.io.File; import java.util.Date; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.timer.api.bean.email.SendMessage; import cn.timer.api.bean.qyzx.QyzxEmpLogin; import cn.timer.api.config.annotation.CurrentUser; import cn.timer.api.config.annotation.UserBean; import cn.timer.api.utils.Result; import cn.timer.api.utils.ResultUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; /** * <p> * Title: EmailController.java * </p> * <p> * Description: * </p> * * @author dsc * @date 2020年5月25日 * @version 1.0 */ @RestController @Api(tags = "1.1 邮件发送") @Transactional @RequestMapping(value = "/email", produces = { "application/json" }) public class EmailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${spring.mail.username}") private String username; @Autowired JavaMailSender mailSender; @PostMapping("/sendSimpleMail") @ApiOperation(value = "发送简单邮件", httpMethod = "POST", notes = "接口发布说明") public Result<Void> sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(content); message.setFrom(username); mailSender.send(message); return ResultUtil.success("邮件发送成功"); } @PostMapping("/sendHtmlMail") @ApiOperation(value = "发送html邮件", httpMethod = "POST", notes = "接口发布说明") public Result<Void> sendHtmlMail(@CurrentUser UserBean userBean, String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(username); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); mailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } if (SendMessage.builder().content(content).createdTime(new Date()).createdUser(userBean.getEmpNum()) .receiverEmail(to).subject(subject).build().insert()) return ResultUtil.success("邮件发送成功"); return ResultUtil.error("邮件发送失败"); } @PostMapping("/sendAttchmentMail") @ApiOperation(value = "发送附件邮件", httpMethod = "POST", notes = "接口发布说明") public Result<Void> sendAttchmentMail(String to, String subject, String content, String filePath) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(username); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName, file); // 可将参数换成数组遍历传送多个附件 mailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return ResultUtil.success("邮件发送成功"); } @PostMapping("/sendInlinResourceMail") @ApiOperation(value = "发送静态图片邮件", httpMethod = "POST", notes = "接口发布说明") public Result<Void> sendInlinResourceMail(String to, String subject, String content, String rscPath, String rscId) { logger.info("发送静态邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId); MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(username); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); logger.info("发送成功"); } catch (MessagingException e) { logger.error("发送失败", e); } return ResultUtil.success("邮件发送成功"); } @Transactional @PostMapping("/bindEmailAddress") @ApiOperation(value = "绑定邮箱", httpMethod = "POST", notes = "接口发布说明") public Result<Void> bindEmailAddress(@CurrentUser UserBean userBean, String to) { Integer empNum = userBean.getEmpNum(); QyzxEmpLogin qyzxEmpLogin = QyzxEmpLogin.builder().id(empNum).build().selectById(); if (qyzxEmpLogin == null) return ResultUtil.error("请确认员工状态"); if (qyzxEmpLogin.getEmail() != null && qyzxEmpLogin.getEmailStatus() != null && qyzxEmpLogin.getEmailStatus() == 1) return ResultUtil.error("该账号已绑定过邮箱"); qyzxEmpLogin.setEmail(to); qyzxEmpLogin.setEmailStatus(0); qyzxEmpLogin.updateById(); return sendHtmlMail(userBean, to, "8小时账号邮箱验证", "<html>\r\n" + "<head>\r\n" + "<meta charset=\"UTF-8\">\r\n" + "</head>\r\n" + "<body>\r\n" + " <h1>您好,感谢绑定<a href=\"http://8timer.cn\">8小时人事管家</a>,请点击下方完成邮箱绑定,感谢支持!<h1>\r\n" + " <br />\r\n" + " <a href=\"http://test-8timer-api.youlingrc.com/login/band/" + empNum + "\">点击这里</a>\r\n" + "</body>\r\n" + "</html>"); } @PostMapping("/sendVerifyMail") @ApiOperation(value = "发送注册验证邮件", httpMethod = "POST", notes = "接口发布说明") public Result<Void> sendVerifyMail(@CurrentUser UserBean userBean, String to) { return sendHtmlMail(userBean, to, "8小时账号邮箱验证", "<html>\r\n" + "<head>\r\n" + "<meta charset=\"UTF-8\">\r\n" + "</head>\r\n" + "<body>\r\n" + " <h1>您好,感谢注册<a href=\"http://8timer.cn\">8小时人事管家</a>,点击下面链接完成验证,感谢支持!<h1>\r\n" + " <br />\r\n" + " <a href=\"https://www.4399.com\">激活账户</a>\r\n" + "</body>\r\n" + "</html>"); } }