| | |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | |
| | | private FreeMarkerConfigurer freeMarkerConfigurer; |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() { |
| | | public void afterPropertiesSet() throws Exception { |
| | | mailSender = new JavaMailSenderImpl(); |
| | | mailSender.setUsername(PropertiesUtil.getProperty("email.username")); |
| | | mailSender.setPassword(PropertiesUtil.getProperty("email.password")); |
| | | mailSender.setHost(PropertiesUtil.getProperty("email.host")); |
| | | // 邮件服务器属性配置 |
| | | Properties javaMailProperties = new Properties(); |
| | | javaMailProperties.setProperty("mail.smtp.port", "465"); |
| | | |
| | | // 场景B:加密端口(465)- 启用SSL,对应服务器加密规则 |
| | | javaMailProperties.setProperty("mail.smtp.auth", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.ssl.enable", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.ssl.protocols", "TLSv1.2"); // 兼容服务器加密协议 |
| | | // 配置SSL Socket工厂(仅加密端口需要) |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.port", "465"); // 端口与smtp.port一致(465) |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.fallback", "false"); // 禁用非SSL降级 |
| | | // 465端口禁用STARTTLS(服务器不支持) |
| | | javaMailProperties.setProperty("mail.smtp.starttls.enable", "false"); |
| | | javaMailProperties.setProperty("mail.smtp.ssl.protocols", "TLSv1.2"); |
| | | |
| | | // 调试模式(生产环境建议改为false) |
| | | javaMailProperties.setProperty("mail.debug", "true"); |
| | | mailSender.setJavaMailProperties(javaMailProperties); |
| | | |
| | | mailMessage = new SimpleMailMessage(); |
| | | mailMessage.setFrom(PropertiesUtil.getProperty("email.from")); |
| | | |
| | | freeMarkerConfigurer = new FreeMarkerConfigurer(); |
| | | freeMarkerConfigurer.setTemplateLoaderPath("classpath:email/ftl"); |
| | | Properties settings = new Properties(); |
| | |
| | | settings.setProperty("default_encoding", "UTF-8"); |
| | | settings.setProperty("locale", "zh_CN"); |
| | | freeMarkerConfigurer.setFreemarkerSettings(settings); |
| | | freeMarkerConfigurer.afterPropertiesSet(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | messageHelper.addAttachment(MimeUtility.encodeWord(emailMessage.getFilename()), rarfile); |
| | | } |
| | | this.mailSender.send(mailMsg);// 发送 |
| | | } catch (MessagingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | } catch (UnsupportedEncodingException e) { |
| | | } catch (MessagingException | UnsupportedEncodingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | } |
| | | } |
| | |
| | | Template tpl = this.freeMarkerConfigurer.getConfiguration().getTemplate(ftlname); |
| | | // 加入map到模板中 输出对应变量 |
| | | html = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (TemplateException e) { |
| | | } catch (IOException | TemplateException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return html; |