package io.renren.modules.email.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.activation.DataSource; import javax.mail.internet.MimeMessage; import javax.mail.util.ByteArrayDataSource; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; public class MailUtil { final static Logger logger = LoggerFactory.getLogger(MailUtil.class); //防止附件文件名过长是发生乱码,因为文件名过长时,会截取,截取后还原文件名字时就会乱码,这个属性,禁止截取 static { System.setProperty("mail.mime.splitlongparameters", "false"); } /** * @param mailSender * @param bytes 文件-----》流------》压缩-----》字节数组 * @param from 发送邮件账号 * @param to 接收邮件账号 * @param subject 主题 * @param content 邮件内容 * @param attachMentFileName zip格式的文件名字 */ public static void sendZipAttachmentEmail(JavaMailSender mailSender, byte[] bytes, String from, String to, String subject, String content, String attachMentFileName) { logger.info("发件人:{},收件人:{}", from, to); InputStream is = new ByteArrayInputStream(bytes); MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); //true 可以解析html helper.setText(content, true); DataSource dataSource = new ByteArrayDataSource(is, "application/zip"); //attachMentFileName是以.zip结尾 helper.addAttachment(attachMentFileName, dataSource); mailSender.send(message); logger.info("带附件的邮件发送成功"); } catch (Exception e) { logger.error(e.toString(), e); logger.info("带附件的邮件发送失败"); } finally { try { is.close(); } catch (IOException e) { logger.error(e.toString(), e); } } } /** * @param filePaths 原文件名称和文件地址的对应关系 * @param mailSender * @param from * @param to * @param subject * @param content * @param attachMentFileName */ public static void sendEmail(Map<String, String> filePaths, JavaMailSender mailSender, String from, String to, String subject, String content, String attachMentFileName) { if (null == filePaths || filePaths.size() == 0) { logger.info("文件路径为空,没有需要发送的文件"); return; } byte[] bytes = ZipCompressor.compress(filePaths); sendZipAttachmentEmail(mailSender, bytes, from, to, subject, content, attachMentFileName); } }
构造JavaMailSender,如果需要根据不同的业务采用不同的邮件服务器,需要分别配置
application.yml文件:
mail: default-encoding1: UTF-8 host1: 192.168.13.10 port1: 25 protocol1: smtp username1: xx@xx.com.cn default-encoding2: UTF-8 host2: 192.168.13.10 port2: 25 protocol2: smtp username2: xx@xx.com.cn
package io.renren.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; /** * 发送邮件的配置 */ @Configuration public class EmailConfig { @Value("${mail.default-encoding1}") private String encoding1; @Value("${mail.host1}") private String host1; @Value("${mail.port1}") private int port1; @Value("${mail.protocol1}") private String protocol1; @Value("${mail.username1}") private String username1; @Value("${mail.default-encoding2}") private String encoding2; @Value("${mail.host2}") private String host2; @Value("${mail.port2}") private int port2; @Value("${mail.protocol2}") private String protocol2; @Value("${mail.username2}") private String username2; @Bean("mailSender1") public JavaMailSender mailSender1() { return setJavaMailSender(host1, username1, port1, protocol1, encoding1); } @Bean("mailSender2") public JavaMailSender mailSender2() { return setJavaMailSender(host2, username2, port2, protocol2, encoding2); } private JavaMailSenderImpl setJavaMailSender(String host, String userName, int port, String protocol, String encoding) { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setUsername(userName); mailSender.setHost(host); mailSender.setPort(port); mailSender.setProtocol(protocol); mailSender.setDefaultEncoding(encoding); return mailSender; } }