Authored by LUOXC

Merge branch 'dev6.9.7' into test6.9.7

@@ -15,6 +15,7 @@ import com.yohoufo.dal.product.ProductMapper; @@ -15,6 +15,7 @@ import com.yohoufo.dal.product.ProductMapper;
15 import com.yohoufo.dal.product.StoragePriceMapper; 15 import com.yohoufo.dal.product.StoragePriceMapper;
16 import com.yohoufo.dal.product.model.Product; 16 import com.yohoufo.dal.product.model.Product;
17 import com.yohoufo.dal.product.model.StoragePrice; 17 import com.yohoufo.dal.product.model.StoragePrice;
  18 +import com.yohoufo.order.utils.MailSender;
18 import lombok.Builder; 19 import lombok.Builder;
19 import lombok.Getter; 20 import lombok.Getter;
20 import lombok.Setter; 21 import lombok.Setter;
@@ -92,7 +93,7 @@ public class HkAccountSettlement { @@ -92,7 +93,7 @@ public class HkAccountSettlement {
92 .reduce(BigDecimal.ZERO, BigDecimal::add); 93 .reduce(BigDecimal.ZERO, BigDecimal::add);
93 log.info("{} settle, sum income is {}", uid, sumIncome); 94 log.info("{} settle, sum income is {}", uid, sumIncome);
94 // 满足打款条件,通知财务打款 95 // 满足打款条件,通知财务打款
95 - double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit",5000); 96 + double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit", 5000);
96 if (sumIncome.doubleValue() >= limitAmount) { 97 if (sumIncome.doubleValue() >= limitAmount) {
97 try { 98 try {
98 log.info("{} settle, send email", uid, sumIncome); 99 log.info("{} settle, send email", uid, sumIncome);
@@ -217,80 +218,15 @@ public class HkAccountSettlement { @@ -217,80 +218,15 @@ public class HkAccountSettlement {
217 218
218 219
219 public void sendMail(List<TradeBillResult> tradeBillResults) throws MessagingException { 220 public void sendMail(List<TradeBillResult> tradeBillResults) throws MessagingException {
220 -  
221 - //环境  
222 - Properties props = new Properties(); // 参数配置  
223 - props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)  
224 - props.setProperty("mail.smtp.host", "smtp.126.com"); // 发件人的邮箱的 SMTP 服务器地址  
225 - props.setProperty("mail.smtp.auth", "true");  
226 -  
227 - // 2. 根据配置创建会话对象, 用于和邮件服务器交互  
228 - Session session = Session.getDefaultInstance(props);  
229 -  
230 - //邮件  
231 - MimeMessage msg = new MimeMessage(session);  
232 - //设置主题  
233 - msg.setSubject("UFO香港卖家结算");  
234 - //发件人,注意中文的处理  
235 - msg.setFrom(new InternetAddress("UFO System<lxc1317@126.com>"));  
236 - //设置邮件回复人  
237 -  
238 - String toMail = configReader.getString("ufo.order.seller.hkAccountSettlementEmailTo","xiuchun.luo@yoho.cn");  
239 - msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));  
240 -  
241 - //整封邮件的MINE消息体  
242 - MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的组合关系  
243 - //设置邮件的MINE消息体  
244 - msg.setContent(msgMultipart);  
245 -  
246 - //附件1  
247 - MimeBodyPart attch1 = new MimeBodyPart();  
248 - //正文内容  
249 - MimeBodyPart content = new MimeBodyPart();  
250 -  
251 - //把内容,附件1,附件2加入到 MINE消息体中  
252 - msgMultipart.addBodyPart(attch1);  
253 - msgMultipart.addBodyPart(content);  
254 -  
255 - //把文件,添加到附件1中  
256 - //数据源  
257 - DataSource dataSource = buildDataSource(tradeBillResults);  
258 - //数据处理器  
259 - DataHandler dataHandler = new DataHandler(dataSource);  
260 - //设置第一个附件的数据  
261 - attch1.setDataHandler(dataHandler);  
262 - //设置第一个附件的文件名  
263 -  
264 - try {  
265 - attch1.setFileName(MimeUtility.encodeText("卖家结算清单.csv"));  
266 - } catch (UnsupportedEncodingException e) {  
267 - attch1.setFileName("list.csv");  
268 - }  
269 -  
270 - //正文(图片和文字部分)  
271 - MimeMultipart bodyMultipart = new MimeMultipart("related");  
272 - //设置内容为正文  
273 - content.setContent(bodyMultipart);  
274 -  
275 - //html代码部分  
276 - MimeBodyPart htmlPart = new MimeBodyPart();  
277 -  
278 - //正文添加图片和html代码  
279 - bodyMultipart.addBodyPart(htmlPart);  
280 -  
281 - //html代码  
282 - htmlPart.setContent("ok", "text/html;charset=utf-8");  
283 -  
284 - //生成文件邮件  
285 - msg.saveChanges();  
286 -  
287 -  
288 - Transport transport = session.getTransport();  
289 - transport.connect("lxc1317@126.com", "luoxiuchun");  
290 - transport.sendMessage(msg, msg.getAllRecipients()); 221 + MailSender.newMailSender()
  222 + .subject("UFO香港卖家结算")
  223 + .to(configReader.getString("ufo.order.seller.hkAccountSettlementEmailTo", "xiuchun.luo@yoho.cn"))
  224 + .body("ok", "text/html;charset=utf-8")
  225 + .attachment("卖家结算清单.csv", buildDate(tradeBillResults), "application/x-csv;charset=utf-8")
  226 + .send();
291 } 227 }
292 228
293 - private ByteArrayDataSource buildDataSource(List<TradeBillResult> tradeBillResults) { 229 + private String buildDate(List<TradeBillResult> tradeBillResults) {
294 StringBuffer text = new StringBuffer(); 230 StringBuffer text = new StringBuffer();
295 text.append("用户UID,订单编号,商品货号,商品名称,商品金额,税费,平台服务费,打款金额,金额类型"); 231 text.append("用户UID,订单编号,商品货号,商品名称,商品金额,税费,平台服务费,打款金额,金额类型");
296 tradeBillResults.forEach(tradeBillResult -> text.append("\n") 232 tradeBillResults.forEach(tradeBillResult -> text.append("\n")
@@ -304,11 +240,7 @@ public class HkAccountSettlement { @@ -304,11 +240,7 @@ public class HkAccountSettlement {
304 .append(tradeBillResult.getPayAmount()).append(",") 240 .append(tradeBillResult.getPayAmount()).append(",")
305 .append(tradeBillResult.getPayType()) 241 .append(tradeBillResult.getPayType())
306 ); 242 );
307 - try {  
308 - return new ByteArrayDataSource(text.toString(), "application/x-csv;charset=utf-8");  
309 - } catch (IOException e) {  
310 - throw new IllegalStateException(e);  
311 - } 243 + return text.toString();
312 } 244 }
313 245
314 } 246 }
  1 +package com.yohoufo.order.utils;
  2 +
  3 +import com.google.common.collect.Lists;
  4 +import lombok.Builder;
  5 +
  6 +import javax.activation.DataHandler;
  7 +import javax.activation.DataSource;
  8 +import javax.mail.Message;
  9 +import javax.mail.MessagingException;
  10 +import javax.mail.Session;
  11 +import javax.mail.Transport;
  12 +import javax.mail.internet.*;
  13 +import javax.mail.util.ByteArrayDataSource;
  14 +import java.io.IOException;
  15 +import java.util.List;
  16 +import java.util.Properties;
  17 +
  18 +public class MailSender {
  19 +
  20 + private Properties properties;
  21 + private String user;
  22 + private String password;
  23 +
  24 + String subject;
  25 + String from;
  26 + String to;
  27 + Part body;
  28 + List<Part> attachments;
  29 +
  30 + public static MailSender newMailSender() {
  31 + return newMailSender("smtp.126.com", "lxc1317@126.com", "luoxiuchun").from("UFO System<lxc1317@126.com>");
  32 + }
  33 +
  34 + public static MailSender newMailSender(String host, String user, String password) {
  35 + Properties properties = new Properties(); // 参数配置
  36 + properties.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
  37 + properties.setProperty("mail.smtp.host", host); // 发件人的邮箱的 SMTP 服务器地址
  38 + properties.setProperty("mail.smtp.auth", "true");
  39 + MailSender mailSender = new MailSender();
  40 + mailSender.properties = properties;
  41 + mailSender.user = user;
  42 + mailSender.password = password;
  43 + return mailSender;
  44 + }
  45 +
  46 + private MailSender() {
  47 + this.attachments = Lists.newArrayList();
  48 + }
  49 +
  50 + public MailSender subject(String subject) {
  51 + this.subject = subject;
  52 + return this;
  53 + }
  54 +
  55 + public MailSender from(String from) {
  56 + this.from = from;
  57 + return this;
  58 + }
  59 +
  60 + public MailSender to(String to) {
  61 + this.to = to;
  62 + return this;
  63 + }
  64 +
  65 + public MailSender body(String content, String contentType) {
  66 + this.body = Part.builder().content(content).contentType(contentType).build();
  67 + return this;
  68 + }
  69 +
  70 + public MailSender attachment(String filename, String content, String contentType) {
  71 + attachments.add(Part.builder().filename(filename).content(content).contentType(contentType).build());
  72 + return this;
  73 + }
  74 +
  75 + public void send() throws MessagingException {
  76 + // 2. 根据配置创建会话对象, 用于和邮件服务器交互
  77 + Session session = Session.getDefaultInstance(properties);
  78 +
  79 + //邮件
  80 + MimeMessage msg = new MimeMessage(session);
  81 + //设置主题
  82 + msg.setSubject(subject);
  83 + //发件人,注意中文的处理
  84 + msg.setFrom(new InternetAddress(from));
  85 + //设置邮件回复人
  86 +
  87 + msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  88 +
  89 + //整封邮件的MINE消息体
  90 + MimeMultipart msgMultipart = new MimeMultipart("mixed");
  91 + //设置邮件的MINE消息体
  92 + msg.setContent(msgMultipart);
  93 +
  94 +
  95 + //正文内容
  96 + MimeBodyPart content = new MimeBodyPart();
  97 +
  98 +
  99 + msgMultipart.addBodyPart(content);
  100 +
  101 + attachments.forEach(attachment -> {
  102 + try {
  103 + MimeBodyPart attachmentBodyPart = new MimeBodyPart();
  104 + msgMultipart.addBodyPart(attachmentBodyPart);
  105 + DataSource dataSource = new ByteArrayDataSource(attachment.content, attachment.contentType);
  106 + DataHandler dataHandler = new DataHandler(dataSource);
  107 + attachmentBodyPart.setDataHandler(dataHandler);
  108 + attachmentBodyPart.setFileName(MimeUtility.encodeText(attachment.filename));
  109 + } catch (MessagingException | IOException e) {
  110 +
  111 + }
  112 + });
  113 +
  114 +
  115 + //正文(图片和文字部分)
  116 + MimeMultipart bodyMultipart = new MimeMultipart("related");
  117 + //设置内容为正文
  118 + content.setContent(bodyMultipart);
  119 +
  120 + //html代码部分
  121 + MimeBodyPart htmlPart = new MimeBodyPart();
  122 +
  123 + //正文添加图片和html代码
  124 + bodyMultipart.addBodyPart(htmlPart);
  125 +
  126 + //html代码
  127 + htmlPart.setContent(body.content, body.contentType);
  128 +
  129 + //生成文件邮件
  130 + msg.saveChanges();
  131 +
  132 +
  133 + Transport transport = session.getTransport();
  134 + transport.connect(user, password);
  135 + transport.sendMessage(msg, msg.getAllRecipients());
  136 + transport.close();
  137 + }
  138 +
  139 + @Builder
  140 + private class Part {
  141 + String filename;
  142 + String content;
  143 + String contentType;
  144 + }
  145 +
  146 +}