EmailController.java 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**  
* <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;
11
import java.util.Date;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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;

29
import cn.timer.api.bean.email.SendMessage;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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}")
邓实川 committed
58
	private String username;
59 60 61 62 63 64 65 66 67 68 69

	@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);
邓实川 committed
70
		message.setFrom(username);
71
		mailSender.send(message);
邓实川 committed
72
		return ResultUtil.success("邮件发送成功");
73 74 75 76
	}

	@PostMapping("/sendHtmlMail")
	@ApiOperation(value = "发送html邮件", httpMethod = "POST", notes = "接口发布说明")
77
	public Result<Void> sendHtmlMail(@CurrentUser UserBean userBean, String to, String subject, String content) {
78 79 80
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
邓实川 committed
81
			helper.setFrom(username);
82 83 84 85
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);
			mailSender.send(message);
86

87 88 89
		} catch (MessagingException e) {
			e.printStackTrace();
		}
90 91 92 93
		if (SendMessage.builder().content(content).createdTime(new Date()).createdUser(userBean.getEmpNum())
				.receiverEmail(to).subject(subject).build().insert())
			return ResultUtil.success("邮件发送成功");
		return ResultUtil.error("邮件发送失败");
94 95 96 97 98 99 100 101
	}

	@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);
邓实川 committed
102
			helper.setFrom(username);
103 104 105 106 107 108 109 110 111 112
			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();
		}
邓实川 committed
113
		return ResultUtil.success("邮件发送成功");
114 115 116 117 118 119 120 121 122
	}

	@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);
邓实川 committed
123
			helper.setFrom(username);
124 125 126 127 128 129 130 131 132 133
			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);
		}
邓实川 committed
134
		return ResultUtil.success("邮件发送成功");
135
	}
136 137

	@Transactional
138 139 140
	@PostMapping("/bindEmailAddress")
	@ApiOperation(value = "绑定邮箱", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> bindEmailAddress(@CurrentUser UserBean userBean, String to) {
141 142 143 144 145 146 147 148 149 150 151 152
		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"
邓实川 committed
153
						+ "	<h1>您好,感谢绑定<a href=\"http://8timer.cn\">8小时人事管家</a>,请点击下方完成邮箱绑定,感谢支持!<h1>\r\n"
154
						+ "	<br />\r\n" + "	<a href=\"http://test-8timer-api.youlingrc.com/login/band/" + empNum
邓实川 committed
155
						+ "\">点击这里</a>\r\n" + "</body>\r\n" + "</html>");
156 157 158 159 160
	}

	@PostMapping("/sendVerifyMail")
	@ApiOperation(value = "发送注册验证邮件", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> sendVerifyMail(@CurrentUser UserBean userBean, String to) {
161 162 163 164
		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>");
165 166 167
	}

}