| | |
| | | |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.GeneralSecurityException; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | import javax.mail.MessagingException; |
| | | import javax.mail.internet.MimeMessage; |
| | | import javax.mail.internet.MimeUtility; |
| | | |
| | | import com.sun.mail.util.MailSSLSocketFactory; |
| | | import email.EmailPropertiesUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.core.io.FileSystemResource; |
| | | import org.springframework.mail.SimpleMailMessage; |
| | | import org.springframework.mail.javamail.JavaMailSenderImpl; |
| | |
| | | import freemarker.template.TemplateException; |
| | | import kernel.util.StringUtils; |
| | | |
| | | public class InternalEmailSenderServiceImpl implements InternalEmailSenderService { |
| | | public class InternalEmailSenderServiceImpl implements InternalEmailSenderService, InitializingBean { |
| | | private JavaMailSenderImpl mailSender; |
| | | private static final Logger logger = LoggerFactory.getLogger(InternalEmailSenderServiceImpl.class); |
| | | private SimpleMailMessage mailMessage; |
| | | private FreeMarkerConfigurer freeMarkerConfigurer; |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws GeneralSecurityException { |
| | | mailSender = new JavaMailSenderImpl(); |
| | | mailSender.setUsername(EmailPropertiesUtil.getProperty("email.username")); |
| | | mailSender.setPassword(EmailPropertiesUtil.getProperty("email.password")); |
| | | mailSender.setHost(EmailPropertiesUtil.getProperty("email.host")); |
| | | Properties javaMailProperties = new Properties(); |
| | | javaMailProperties.setProperty("mail.smtp.port", "465"); |
| | | javaMailProperties.setProperty("mail.smtp.starttls.enable", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.auth", "true"); |
| | | javaMailProperties.setProperty("mmail.debug", "true"); |
| | | javaMailProperties.setProperty("mail.smtp.host", "smtp.gmail.com"); |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
| | | javaMailProperties.setProperty("mail.smtp.socketFactory.port", "465"); |
| | | MailSSLSocketFactory sf = new MailSSLSocketFactory("TLSv1.2"); |
| | | sf.setTrustAllHosts(true); |
| | | javaMailProperties.put("mail.smtp.ssl.socketFactory",sf); |
| | | javaMailProperties.setProperty("mail.smtp.ssl.portocols","TLSv1.2"); |
| | | mailSender.setJavaMailProperties(javaMailProperties); |
| | | mailMessage = new SimpleMailMessage(); |
| | | mailMessage.setFrom(EmailPropertiesUtil.getProperty("email.from")); |
| | | freeMarkerConfigurer = new FreeMarkerConfigurer(); |
| | | freeMarkerConfigurer.setTemplateLoaderPath("classpath:email/ftl"); |
| | | Properties settings = new Properties(); |
| | | settings.setProperty("template_update_delay", "1800"); |
| | | settings.setProperty("default_encoding", "UTF-8"); |
| | | settings.setProperty("locale", "zh_CN"); |
| | | freeMarkerConfigurer.setFreemarkerSettings(settings); |
| | | } |
| | | |
| | | @Override |
| | | public void send(EmailMessage emailMessage) { |
| | | try { |
| | | |
| | | logger.info("-----进到邮件发送-----"); |
| | | MimeMessage mailMsg = this.mailSender.createMimeMessage(); |
| | | |
| | | logger.info("----------邮件发送,接收邮箱:"+emailMessage.getTomail()+"发送邮箱:"+this.mailMessage.getFrom()); |
| | | MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg, true, "UTF-8"); |
| | | messageHelper.setTo(emailMessage.getTomail());// 接收邮箱 |
| | | messageHelper.setFrom(this.mailMessage.getFrom());// 发送邮箱 |
| | |
| | | } else { |
| | | messageHelper.setText(this.getMailText(emailMessage.getFtlname(), emailMessage.getMap()), true);// 邮件内容 |
| | | } |
| | | |
| | | logger.info("-----1111111-----"); |
| | | // true 表示启动HTML格式的邮件 |
| | | if (emailMessage.getFile() != null) { |
| | | // 添加邮件附件 |
| | |
| | | // 使用MimeUtility.encodeWord 解决附件名中文乱码的问题 |
| | | messageHelper.addAttachment(MimeUtility.encodeWord(emailMessage.getFilename()), rarfile); |
| | | } |
| | | |
| | | logger.info("-----邮件开始发送-----开始"); |
| | | this.mailSender.send(mailMsg);// 发送 |
| | | |
| | | logger.info("-----邮件开始发送-----成功"); |
| | | } catch (MessagingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | logger.error("邮件发送失败【MessagingException】"+e.getMessage(), e); |
| | | } catch (UnsupportedEncodingException e) { |
| | | logger.error(e.getMessage(), e); |
| | | logger.error("邮件发送失败【UnsupportedEncodingException】"+e.getMessage(), e); |
| | | }catch (Exception e){ |
| | | logger.error("邮件发送失败【Exception】"+"邮件发送失败"+e.getMessage(), e); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取模板并将内容输出到模板 |
| | | * |
| | | * |
| | | * @param content |
| | | * @return |
| | | */ |