655e7a7414071aea80946e20bab966286584b31b.svn-base
32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
package com.espeed.yxy.mail;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.espeed.centre.pojo.CentreTrackSet;
import com.espeed.centre.pojo.CentreYxyedmNewcustomer;
import com.espeed.centre.pojo.CentreYxyedmOldcustomer;
import com.espeed.yxy.pojo.YxySendMailDetail;
import com.espeed.yxy.pojo.YxySendMailMaster;
import com.espeed.yxy.pojo.YxySendSmtpInfo;
import com.espeed.yxy.service.YxySendMailService;
import com.espeed.yxy.tool.ConfigPath;
import com.espeed.yxy.tool.LogsTool;
import com.espeed.yxy.util.AnalysisMail;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: 邮件发送方法(营销游)
* 开始时间: 2013.12.09
* 程 序 员: 谢勇
* 最后修改:
* 备 注: 如需修改请通知程序员
*/
public class YxySendMail {
//个人版营销邮的域名集合
private static String[] domainArray = new String[]{
"waimaoyou.cn","tomex.com.cn","mkonegroup.com","cposl.com",
"chinastonefactory.com","bf6-led.com","yexinglonggroup.com","wheelnutcn.com",
"aokwater.com","hesunny-international.com","szkring.com","sumaonetting.com",
"trsolidcontrol.com","ywksmy.com","sschgroup.com","tf_accessories.com",
"moeatsu.com","chipsen.com","aishine.com","cnbigjeans.com"
};
/**发送邮件(业务判断)*/
public void sendMailMethod(List<YxySendMailDetail> addressList,YxySendMailService sendmailservice,YxySendSmtpInfo SmtpInfo,YxySendMailMaster mailinfo,Properties props,Transport transport){
//变量定义
int mailid=0;//邮件ID
String mailuid="";//邮件UID
int addressid=0;//地址ID
int expnum=0;//地址异常量
try {
mailid=mailinfo.getSend_mail_id();//邮件ID
mailuid=mailinfo.getMail_uid();//邮件UID
//变量赋值
MimeMessage mimeMsg = null; //MIME邮件对象
String servername=SmtpInfo.getSmtp_service_account();//smtp账号
javax.mail.Session mailSession = null;
//是自配还是运营
//int isyxy=SmtpInfo.getSmtp_service_isyxy();
//此邮件所属用户ID
String loginid=mailinfo.getUser_loginid();
loginid=loginid.replaceAll(" ", "");
String domain=mailinfo.getUser_domain();
domain=domain.replaceAll(" ", "");
//用户eml文件路径
String useremlpath=sendmailservice.findCompanyParamByCode("yxy_user_data_save_path",loginid,domain);
if(useremlpath==null||useremlpath.equals("")||useremlpath.equals("-1")){
//系统级
useremlpath=sendmailservice.findCompanyParamByCode("yxy_domain_save_path",loginid,domain);
}
mailSession = javax.mail.Session.getInstance(props);
//以下判断是个人版营销邮还是易外销营销邮
String centersys=ConfigPath.getYxyServicePath();//默认是易外销营销邮
if(Arrays.asList(domainArray).contains(mailinfo.getUser_domain())){//是单独营销邮
centersys=ConfigPath.getYxyServicePath2();
}
//3.收件人
InternetAddress[] sendTo = new InternetAddress[addressList.size()];
//4.加入点读服务器访问地址
String nurl = "";
if(mailinfo.getIsold()==0 || mailinfo.getIsold()==5 || mailinfo.getIsold()==6)//新客户营销
{
nurl=ConfigPath.getClickService();
}
else if(mailinfo.getIsold()==1 || mailinfo.getIsold()==4)//老客户跟进或CRM库超标发送
{
nurl=ConfigPath.getClickService2();
}
else if(mailinfo.getIsold()==2 || mailinfo.getIsold()==3)//代发模式或自有域名
{
//获取跟踪配置信息
String track_hql = "from CentreTrackSet where company_doamin = '"+mailinfo.getUser_domain()+"' and type = 1 and status = 1";
List<CentreTrackSet> tracks = sendmailservice.getTrackSetByHql(track_hql);
if(tracks.size() > 0){//有跟踪配置信息则使用配置里面的链接
nurl = "http://"+tracks.get(0).getDomain()+"/";
}else{//否则使用默认的老客户跟进链接
nurl=ConfigPath.getClickService2();
}
}
else{//否则取默认的老客户跟进链接
nurl=ConfigPath.getClickService2();
}
//6.解析eml文件获取内容
AnalysisMail paxlmail=new AnalysisMail();
String emlpath=mailinfo.getEml_file_path().replace("#####", useremlpath);
List paxlresult=paxlmail.ParseEmlMethod(emlpath, mailinfo.getEml_encode_key());
//内容
String mailcontext=paxlresult.get(0).toString();
//是否加入链接地址
String linkaddress="";
if(mailinfo.getInclude_link()==1){
if(mailinfo.getUnsubscribe_language()==1){//英文
linkaddress= "<p align=\"center\" style=\"color: #999999;font-size: 12px;\">Access to more detailed :" +
"<a href=\""+ mailinfo.getLink_context() + "\">" + "webpage"
+ "</a></p>";
}else{
linkaddress= "<p align=\"center\" style=\"color: #999999;font-size: 12px;\">如无法正常显示,请点击:<a href=\""
+ mailinfo.getLink_context() + "\">" + "网页链接"
+ "</a></p>";
}
mailcontext=mailcontext+linkaddress;
}
//设置from、to等信息
mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
//之前的新客户营销、之前的老客户跟进、CRM库自有域名、CRM库超标、新客户营销-自有域名
if(mailinfo.getIsold()==0 ||
mailinfo.getIsold()==1 ||
mailinfo.getIsold()==3 ||
mailinfo.getIsold()==4 ||
mailinfo.getIsold()==5){
//发件人
String sender = mailinfo.getSender();
//发件人地址
String sendEmail = mailinfo.getSend_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}else if(mailinfo.getIsold()==2){//CRM库代发模式
String hql = "from CentreYxyedmOldcustomer where company_domain = '"+domain+"'";
List<CentreYxyedmOldcustomer> oldset = sendmailservice.getOldCustomerSet(hql);
if(oldset.size() > 0){//有配置信息
//发件人
String sender = oldset.get(0).getSender_name();
//发件人地址
String sendEmail = oldset.get(0).getSender_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}else{//无配置信息
//发件人
String sender = mailinfo.getSender();
//发件人地址
String sendEmail = mailinfo.getSend_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}
}else if(mailinfo.getIsold()==6){//新客户营销-自有域名超标或代发模式没超标
String hql = "from CentreYxyedmNewcustomer where company_domain = '"+domain+"'";
List<CentreYxyedmNewcustomer> newset = sendmailservice.getNewCustomerSet(hql);
if(newset.size() > 0){//有配置信息
if(newset.get(0).getType() == 1){//代发模式没超标
//发件人
String sender = newset.get(0).getSender_name();
//发件人地址
String sendEmail = newset.get(0).getSender_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}else{//自有域名超标
//发件人
String sender = mailinfo.getSender();
//发件人地址
String sendEmail = mailinfo.getSend_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}
}else{//无配置信息
//发件人
String sender = mailinfo.getSender();
//发件人地址
String sendEmail = mailinfo.getSend_email().replace("_", "");
//回复地址
String replyEmail = mailinfo.getReply_email().replace("_", "");
//邮件中的sender
InternetAddress senderAddr = new InternetAddress(servername,sender,"UTF-8");
mimeMsg.setSender(senderAddr);
//邮件中的from
InternetAddress fromAddr = new InternetAddress(sendEmail,sender,"UTF-8");
mimeMsg.setFrom(fromAddr);
//邮件中的reply
InternetAddress replyAddr = new InternetAddress(replyEmail);
mimeMsg.setReplyTo(new InternetAddress[]{replyAddr});
}
}
//设置信件头的发送日期
mimeMsg.setSentDate(new Date());
//定义附件
String parstattr="";
String attrPath[]=null;
String attrName[]=null;
if(mailinfo.getAttarchment_path()!=null&&!mailinfo.getAttarchment_path().equals("")){
//包含的多个附件拆解
String[] splitPath=mailinfo.getAttarchment_path().split("##P##");
if(splitPath.length>=2){
attrPath=splitPath[1].split(",");//定义附件名
attrName=new String[attrPath.length];
for(int a=0;a<attrPath.length;a++){
String[] splitStr=attrPath[a].split("#####");
attrPath[a]=centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\attachment\\"+splitStr[0];//附件路径
attrName[a]=splitStr[1];
//判断是否存在文件
File f=new File(attrPath[a]);
if(!f.exists()){//如果不存在则重新解析
parstattr=paxlmail.parsePicOrAttr(emlpath, mailinfo.getEml_encode_key(),1, centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\attachment\\");
String[] attrresult=parstattr.split(",");//多个分割
parstattr=centersys+"##P##"+parstattr;
attrPath=new String[attrresult.length];
attrName=new String[attrresult.length];
for(int s=0;s<attrresult.length;s++){
String[] attrname=attrresult[s].split("#####");
attrPath[s]=centersys+"\\"+attrname[0];
attrName[s]=attrname[1];
}
break;
}
}
}
}
//定义图片part
String [] picPath=null;
String parstpic="";
if(mailinfo.getImage_path()!=null&&!mailinfo.getImage_path().equals("")){
picPath=mailinfo.getImage_path().split("#####");
for(int i=0;i<picPath.length;i++){
String picpath=centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\pic\\"+picPath[i];
//判断图片是否存在
File f=new File(picpath);
if(!f.exists()){//如果不存在则重新解析
parstpic=paxlmail.parsePicOrAttr(emlpath, mailinfo.getEml_encode_key(), 2, centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\pic\\");
picPath=parstpic.split("#####");
break;
}
}
}
//更新邮件信息
sendmailservice.updatepicattrpath(mailid, parstpic, parstattr);
//内容处理
//1.内容加入点击地址
mailcontext+="<img alt=\"trackimg\" src=\"" + nurl + "clickesemail.jsp?to=#RcevieEmail#&mailuid="+mailuid+"&loginid="+loginid+"&domain="+domain+"\" width=0 height=0 />";
//2.判断是否加入退订
String linkback="";
//Header中的退订信息
String listUnsubscribeStr="";
if(mailinfo.getIs_unsubscribe()==1){
listUnsubscribeStr=nurl+"Unsubscribe.jsp?loginid="+loginid+"&domain="+domain+"&mailuid="+mailuid+"&address=#RcevieEmail#";
//显示中文还是英文
if(mailinfo.getUnsubscribe_language()==2){//英文
linkback= "<p align=\"center\" style=\"olor: #999999;font-size: 12px;\">If you don't want to receive these emails in the future,<a href=\"" + nurl
+ "Unsubscribe.jsp?loginid="
+ loginid
+ "&domain=" + domain
+ "&mailuid=" + mailuid
+ "&address=#RcevieEmail#"
+ "\">" + "please.Unsubscribe,Thanks!" + "</a></p>";
}else if(mailinfo.getUnsubscribe_language()==1){//中文
linkback= "<p align=\"center\" style=\"color: #999999;font-size: 12px;\">如果您不需要此邮件,请点击:<a href=\"" + nurl
+ "Unsubscribe.jsp?loginid="
+ loginid
+ "&domain=" + domain
+ "&mailuid=" + mailuid
+ "&address=#RcevieEmail#"
+ "\">" + "退订" + "</a></p>";
}else if(mailinfo.getUnsubscribe_language()==3){//自定义
linkback= "<p align=\"center\" style=\"color: #999999;font-size: 12px;\"><a href=\"" + nurl
+ "Unsubscribe.jsp?loginid="
+ loginid
+ "&domain=" + domain
+ "&mailuid=" + mailuid
+ "&address=#RcevieEmail#"
+ "\">" + mailinfo.getUnstr() + "</a></p>";
}
mailcontext+=linkback;
}
String subject=mailinfo.getSubject();
//设置X-Mailer
mimeMsg.setHeader("X-Mailer", "Yiwaixiao-EDM - SendMailer2.0");
//获取用户信息
int user_id = 0;
int company_id = 0;
try{
String sql = "select duser.user_id,dcom.company_id from centre_user as duser " +
"LEFT JOIN centre_company as dcom on duser.company_id = dcom.company_id " +
"where duser.login_id = '"+loginid+"' and dcom.domain = '"+domain+"'";
List<Object> objects = sendmailservice.getUserInfoBySql(sql);
if(objects.size() > 0){
Object[] obj_arr = (Object[])objects.get(0);
if(null!=obj_arr[0]){
user_id = Integer.parseInt(obj_arr[0].toString());
}
if(null!=obj_arr[1]){
company_id = Integer.parseInt(obj_arr[1].toString());
}
}
}catch (Exception e) {
}
//循环发送邮件
for(int i=0;i<addressList.size();i++){
try{
String newListUnsubscribeStr=listUnsubscribeStr;
addressid=addressList.get(i).getMail_id();//地址ID
expnum=addressList.get(i).getIsexp();//异常的量
String body="";
String title="";
//收件人姓名
String receiveName=addressList.get(i).getReceiver();
//企业名
String company_variable="";
if(addressList.get(i).getCompany_variable()!=null){
company_variable=addressList.get(i).getCompany_variable();
}
//变量名
String variable="";
if(addressList.get(i).getVariable()!=null){
variable=addressList.get(i).getVariable();
}
//加入内容高级变量
body=mailcontext;
boolean bodyok=true;
while(bodyok){
if(body.indexOf("%ReceiverName%")!=-1){
//过滤特殊字符
body=body.replace("%ReceiverName%", receiveName);
}else{
bodyok=false;
}
}
boolean receok=true;
//替换点读收件人
while(receok){
if(body.indexOf("#RcevieEmail#")!=-1){
newListUnsubscribeStr=listUnsubscribeStr.replace("#RcevieEmail#", addressList.get(i).getEmail());//替换收件人
body=body.replace("#RcevieEmail#", addressList.get(i).getEmail());//替换收件人
}else{
receok=false;
}
}
//内容企业变量
boolean bodycompanuok=true;
//替换相关信息
while(bodycompanuok){
if(body.indexOf("%titlecompany%")!=-1){
body=body.replace("%titlecompany%",company_variable);//替换收件人
}else{
bodycompanuok=false;
}
}
//内容自定义变量
boolean bodyselfok=true;
//替换相关信息
while(bodyselfok){
if(body.indexOf("%customesubject%")!=-1){
body=body.replace("%customesubject%",variable);//替换收件人
}else{
bodyselfok=false;
}
}
//加入主题,是否选择了多主题
if(mailinfo.getSubject().indexOf("###_")!=-1){
String []titlevalue=mailinfo.getSubject().split("###_");
Random random = new Random();
int number = random.nextInt(titlevalue.length);
subject=titlevalue[number];
}
title=subject;
//主题姓名变量
boolean titleok=true;
while(titleok){
if(title.indexOf("%ReceiverName%")!=-1){
title=title.replace("%ReceiverName%", receiveName);
}else{
titleok=false;
}
}
//主题企业变量
boolean titlecomok=true;
while(titlecomok){
if(title.indexOf("%titlecompany%")!=-1){
title=title.replace("%titlecompany%", company_variable);
}else{
titlecomok=false;
}
}
//主题自定义变量
boolean titlemyok=true;
while(titlemyok){
if(title.indexOf("%customesubject%")!=-1){
title=title.replace("%customesubject%", variable);
}else{
titlemyok=false;
}
}
//设置List-Unsubscribe
mimeMsg.removeHeader("List-Unsubscribe");
mimeMsg.addHeader("List-Unsubscribe", "<"+newListUnsubscribeStr+">");
//主题加入邮件实体中
//替换推广链接信息
body=body.replace("?edmtitle=", "?edmtitle="+title);
mimeMsg.setSubject(title,"UTF-8");
//收件人
sendTo[i] = new InternetAddress(addressList.get(i).getEmail().replace(" ", ""));
//接收者地址
mimeMsg.setRecipient(Message.RecipientType.TO,sendTo[i]);
//将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
MimeMultipart multipart = null;
//不同情况定义不同的邮件类型
if(attrPath!=null&&attrPath.length>0){//有附件
multipart = new MimeMultipart("mixed");
if(picPath!=null&&picPath.length>0){//有图片
//内容|图片MimeBodyPart
MimeBodyPart textAndPicBody = new MimeBodyPart();
//内容|图片Multipart
MimeMultipart textAndPicMultipart = new MimeMultipart("related");
//内容MimeBodyPart
MimeBodyPart textBody = new MimeBodyPart();
//内容Multipart,包含plain和htmnl
MimeMultipart textMultipart = new MimeMultipart("alternative");
//plain部分
MimeBodyPart plainBody = new MimeBodyPart();
String newPlainBody = body;
newPlainBody = newPlainBody.replaceAll("</?[^>]+>", "");
newPlainBody = newPlainBody.replace(" ", "");
newPlainBody = newPlainBody.replace(" ", "");
plainBody.setContent(newPlainBody, "text/plain;charset=UTF-8");
plainBody.setHeader("Content-Transfer-Encoding", "base64");
textMultipart.addBodyPart(plainBody);
//html部分
MimeBodyPart htmlBody = new MimeBodyPart();
//替换跟踪统计信息
if(body.indexOf("ywx-mark-track=\"true\"")!=-1){
String newHtmlBody = body;
newHtmlBody = rebuildOriginalMail(newHtmlBody,addressList.get(i).getEmail(),mailuid,user_id,company_id);
htmlBody.setContent(newHtmlBody, "text/html;charset=UTF-8");
}else{
htmlBody.setContent(body, "text/html;charset=UTF-8");
}
htmlBody.setHeader("Content-Transfer-Encoding", "quoted-printable");
textMultipart.addBodyPart(htmlBody);
textBody.setContent(textMultipart);
textAndPicMultipart.addBodyPart(textBody);
//添加图片
String pic="";
for(int y=0;y<picPath.length;y++){
if(picPath[y]!=null){
pic+=centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\pic\\"+picPath[y]+",";
}
}
String[] picpath=pic.split(",");
for(int j=0;j<picpath.length;j++){
if(!picpath[j].equals("")){
//正文的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(picpath[j]);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setFileName(fds.getName());
jpgBody.setContentID("<IMG"+j+">");
jpgBody.setHeader("Content-Type", "image/jpeg;name="+fds.getName());
jpgBody.removeHeader("Content-Disposition");
textAndPicMultipart.addBodyPart(jpgBody);
}
}
textAndPicBody.setContent(textAndPicMultipart);
multipart.addBodyPart(textAndPicBody);
}else{//无图片
//内容部分
MimeBodyPart textBody = new MimeBodyPart();
MimeMultipart textMultipart = new MimeMultipart("alternative");
//plain部分
MimeBodyPart plainBody = new MimeBodyPart();
String newPlainBody = body;
newPlainBody = newPlainBody.replaceAll("</?[^>]+>", "");
newPlainBody = newPlainBody.replace(" ", "");
newPlainBody = newPlainBody.replace(" ", "");
plainBody.setContent(newPlainBody, "text/plain;charset=UTF-8");
plainBody.setHeader("Content-Transfer-Encoding", "base64");
textMultipart.addBodyPart(plainBody);
//html部分
//替换跟踪统计信息
MimeBodyPart htmlBody = new MimeBodyPart();
if(body.indexOf("ywx-mark-track=\"true\"")!=-1){
String newHtmlBody = body;
newHtmlBody = rebuildOriginalMail(newHtmlBody,addressList.get(i).getEmail(),mailuid,user_id,company_id);
htmlBody.setContent(newHtmlBody, "text/html;charset=UTF-8");
}else{
htmlBody.setContent(body, "text/html;charset=UTF-8");
}
htmlBody.setHeader("Content-Transfer-Encoding", "quoted-printable");
textMultipart.addBodyPart(htmlBody);
textBody.setContent(textMultipart);
multipart.addBodyPart(textBody);
}
for(int s=0;s<attrPath.length;s++){
MimeBodyPart attachment = createAttachment(attrPath[s],attrName[s]);
multipart.addBodyPart(attachment);
}
}else if(picPath!=null&&picPath.length>0){//有图片
multipart = new MimeMultipart("related");
//内容部分
MimeBodyPart textBody = new MimeBodyPart();
MimeMultipart textMultipart = new MimeMultipart("alternative");
//plain部分
MimeBodyPart plainBody = new MimeBodyPart();
String newPlainBody = body;
newPlainBody = newPlainBody.replaceAll("</?[^>]+>", "");
newPlainBody = newPlainBody.replace(" ", "");
newPlainBody = newPlainBody.replace(" ", "");
plainBody.setContent(newPlainBody, "text/plain;charset=UTF-8");
plainBody.setHeader("Content-Transfer-Encoding", "base64");
textMultipart.addBodyPart(plainBody);
//html部分
//替换跟踪统计信息
MimeBodyPart htmlBody = new MimeBodyPart();
if(body.indexOf("ywx-mark-track=\"true\"")!=-1){
String newHtmlBody = body;
newHtmlBody = rebuildOriginalMail(newHtmlBody,addressList.get(i).getEmail(),mailuid,user_id,company_id);
htmlBody.setContent(newHtmlBody, "text/html;charset=UTF-8");
}else{
htmlBody.setContent(body, "text/html;charset=UTF-8");
}
htmlBody.setHeader("Content-Transfer-Encoding", "quoted-printable");
textMultipart.addBodyPart(htmlBody);
textBody.setContent(textMultipart);
multipart.addBodyPart(textBody);
//添加图片
String pic="";
for(int y=0;y<picPath.length;y++){
if(picPath[y]!=null){
pic+=centersys+"\\mailtempfile\\"+domain+"\\"+loginid+"\\pic\\"+picPath[y]+",";
}
}
String[] picpath=pic.split(",");
for(int j=0;j<picpath.length;j++){
if(!picpath[j].equals("")){
//正文的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(picpath[j]);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setFileName(fds.getName());
jpgBody.setContentID("<IMG"+j+">");
jpgBody.setHeader("Content-Type", "image/jpeg;name="+fds.getName());
jpgBody.removeHeader("Content-Disposition");
multipart.addBodyPart(jpgBody);
}
}
}else{//无附件无图片
multipart = new MimeMultipart("alternative");
//plain部分
MimeBodyPart plainBody = new MimeBodyPart();
String newPlainBody = body;
newPlainBody = newPlainBody.replaceAll("</?[^>]+>", "");
newPlainBody = newPlainBody.replace(" ", "");
newPlainBody = newPlainBody.replace(" ", "");
plainBody.setContent(newPlainBody, "text/plain;charset=UTF-8");
plainBody.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(plainBody);
//html部分
//替换跟踪统计信息
MimeBodyPart htmlBody = new MimeBodyPart();
if(body.indexOf("ywx-mark-track=\"true\"")!=-1){
String newHtmlBody = body;
newHtmlBody = rebuildOriginalMail(newHtmlBody,addressList.get(i).getEmail(),mailuid,user_id,company_id);
htmlBody.setContent(newHtmlBody, "text/html;charset=UTF-8");
}else{
htmlBody.setContent(body, "text/html;charset=UTF-8");
}
htmlBody.setHeader("Content-Transfer-Encoding", "quoted-printable");
multipart.addBodyPart(htmlBody);
}
//将上面混合型的 MimeMultipart 对象作为邮件内容并保存
mimeMsg.setContent(multipart);
mimeMsg.saveChanges();
//发送邮件
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
//发送成功后修改该地址为已发送
sendmailservice.updateMailDetailstatus(addressid);
//写入日志
LogsTool.sendLogs(domain,loginid,"服务器:"+SmtpInfo.getSmtp_service_number()+"邮件ID:"+mailid+" 收件人:"+addressList.get(i).getEmail()+"发送成功");
//输出到控制台
System.out.println("邮件ID:"+mailid+"用户名:"+loginid+"@"+domain+"第"+i+"封总共"+addressList.size()+"封,服务器"+SmtpInfo.getSmtp_service_number()+"发送成功!");
}catch (Exception e) {
//出现异常后更新此地址为异常地址
expnum=expnum+1;
sendmailservice.updateExp(addressid,expnum);
//写入日志
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
LogsTool.erroLogs("服务器:"+SmtpInfo.getSmtp_service_number()+"邮件ID为:"+mailid+"地址ID为:"+addressid+"地址发送异常!"+str);
continue;
}
}
System.out.println("邮件ID为:"+mailid+"所有的都发完了");
// if(mailinfo.getIsold()==2)//开启的是独立域名发送
// {
// int smtpsendnum = ConfigPath.getSmtpSendNum();//获取配置文件中的smtp发送数量
//
// System.out.println("邮件ID为:"+mailid+"已经发送"+smtpsendnum+"封");
// }
// else
// {
// System.out.println("邮件ID为:"+mailid+"所有的都发完了");
// }
} catch (Exception e) {
//写入日志
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
LogsTool.erroLogs("邮件ID为:"+mailid+"邮件发送异常!"+str);
}finally{
try {
//更新邮件状态
//循环完成后判断此邮件是否为已发
boolean ishave=sendmailservice.isHaveAddress(mailid);
if(ishave){//还有
sendmailservice.updateMailStatus(0,mailid,mailuid);
}else{//没有了
sendmailservice.updateMailStatus(1,mailid,mailuid);
}
} catch (Exception e2) {
//写入日志
StringWriter sw = new StringWriter();
e2.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
LogsTool.erroLogs("邮件发送方法异常!"+str);
}
}
}
/**根据传入的文件路径创建附件并返回*/
public MimeBodyPart createAttachment(String filepath,String fileName) throws Exception {
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filepath);
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName(MimeUtility.encodeText(fileName,"UTF-8","B"));
return attachmentPart;
}
/**根据传入的邮件正文body和文件路径创建图文并茂的正文部分 */
public MimeBodyPart createContent(String body,String fileName)
throws Exception {
// 用于保存最终正文部分
MimeBodyPart contentBody = new MimeBodyPart();
// 用于组合文本和图片,"related"型的MimeMultipart对象
MimeMultipart contentMulti = new MimeMultipart("related");
//MimeMultipart contentMulti = new MimeMultipart("alternative");
// 正文的文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=UTF-8");
textBody.setHeader("Content-Transfer-Encoding", "quoted-printable");
contentMulti.addBodyPart(textBody);
// // 正文的plain文本部分
// MimeBodyPart textBody2 = new MimeBodyPart();
// String newbody = body.replaceAll("</?[^>]+>", "");
// textBody2.setContent(newbody, "text/plain;charset=UTF-8");
// textBody2.setHeader("Content-Transfer-Encoding", "base64");
// contentMulti.addBodyPart(textBody2);
//图片部分
if(!fileName.equals("")){
String[] picpath=fileName.split(",");
for(int i=1;i<picpath.length+1;i++){
if(!picpath[i-1].equals("")){
//正文的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(picpath[i-1]);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("<IMG"+i+">");
contentMulti.addBodyPart(jpgBody);
}
}
}
// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
contentBody.setContent(contentMulti);
return contentBody;
}
//替换跟踪信息
private String rebuildOriginalMail(String body, String to_mail,
String mail_uid, int user_id, int company_id) throws Exception {
Document doc = Jsoup.parse(body);
Elements elements = doc.select("a[ywx-mark-track=true]");
if (elements.size() > 0) {
Set<String> set = new HashSet<String>();
String href = null;
String url = null;
for (Element element : elements) {
href = element.attr("href");
if (href != null && !href.trim().isEmpty()) {
set.add(href.trim() + "@espeed@" + element.text());
}
}
if (set.size() > 0) {
String[] hres = null;
for (String hre : set) {
hres = hre.split("@espeed@");
url = ConfigPath.getTrackUrl() + "/chain.jsp?ywxType=3&ywxUid="
+ user_id + "&ywxCid=" + company_id + "&ywxMid="
+ mail_uid + "&ywxMail=" + to_mail + "&ywxTo="
+ URLEncoder.encode(hres[0], "UTF-8");
if (hres.length > 1) {
url += "&ywxTitle=" + URLEncoder.encode(hres[1], "UTF-8");
}
//body = body.replace(hres[0], url);
body = body.replace("<a href=\""+hres[0]+"\"", "<a href=\""+url+"\"");
}
}
}
return body;
}
}