@author Exrickx
| 18 | * @author Exrickx |
| 19 | */ |
| 20 | @Component |
| 21 | public class EmailUtils { |
| 22 | |
| 23 | private static final Logger log = LoggerFactory.getLogger(EmailUtils.class); |
| 24 | |
| 25 | @Autowired |
| 26 | private JavaMailSender mailSender; |
| 27 | |
| 28 | @Autowired |
| 29 | private TemplateEngine templateEngine; |
| 30 | |
| 31 | /** |
| 32 | * 发送模版邮件 |
| 33 | * @param sender |
| 34 | * @param sendto |
| 35 | * @param templateName |
| 36 | * @param o |
| 37 | */ |
| 38 | @Async |
| 39 | public void sendTemplateMail(String sender, String sendto,String title, String templateName,Object o) { |
| 40 | |
| 41 | log.info("开始给"+sendto+"发送邮件"); |
| 42 | MimeMessage message = mailSender.createMimeMessage(); |
| 43 | try { |
| 44 | //true表示需要创建一个multipart message html内容 |
| 45 | MimeMessageHelper helper = new MimeMessageHelper(message, true); |
| 46 | helper.setFrom(sender); |
| 47 | helper.setTo(sendto); |
| 48 | helper.setSubject(title); |
| 49 | |
| 50 | Context context = new Context(); |
| 51 | context.setVariable("title",title); |
| 52 | context.setVariables(StringUtils.beanToMap(o)); |
| 53 | //获取模板html代码 |
| 54 | String content = templateEngine.process(templateName, context); |
| 55 | |
| 56 | helper.setText(content, true); |
| 57 | |
| 58 | mailSender.send(message); |
| 59 | log.info("给"+sendto+"发送邮件成功"); |
| 60 | }catch (Exception e){ |
| 61 | e.printStackTrace(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * 验证邮箱 |
| 67 | * @param email |
| 68 | * @return |
| 69 | */ |
| 70 | public static boolean checkEmail(String email) { |
| 71 | boolean flag = false; |
| 72 | try { |
| 73 | String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; |
| 74 | Pattern regex = Pattern.compile(check); |
| 75 | Matcher matcher = regex.matcher(email); |
| 76 | flag = matcher.matches(); |
| 77 | } catch (Exception e) { |
nothing calls this directly
no outgoing calls
no test coverage detected