1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
void
SyncContactUserInfo() {
CustomRunable customeRunable;
List<Map<String, Object>> tempPagedDataList =
this
.GetContactUserList();
List<Map<String, Object>> dataList =
this
.GetContactUserList();
int
totalSize = dataList.size();
if
(totalSize ==
0
)
return
;
int
threadCount = totalSize % PageSize ==
0
? totalSize / PageSize
: totalSize / PageSize +
1
;
for
(
int
i =
1
; i <= threadCount; i++) {
if
(i == threadCount) {
tempPagedDataList = dataList.subList((i -
1
) * PageSize,
(i -
1
) * PageSize + totalSize % PageSize -
1
);
}
else
{
tempPagedDataList = dataList.subList((i -
1
) * PageSize,
PageSize * i - i);
}
customeRunable =
new
CustomRunable(tempPagedDataList, i);
handler.post(customeRunable);
}
}
|
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
|
class
CustomRunable
implements
Runnable {
StringBuilder strBuilder =
new
StringBuilder();
List<Map<String, Object>> dataList;
int
index;
public
CustomRunable(List<Map<String, Object>> dataList,
int
index) {
this
.dataList = dataList;
this
.index = index;
}
public
void
run() {
strBuilder.append(
"<html><body><table style='border:1px solid black'>"
);
strBuilder.append(
"<tr><th>姓名</th><th>手机号码</th></tr>"
);
if
(
this
.dataList !=
null
&&
this
.dataList.size() >
0
) {
for
(Map<String, Object> data : dataList) {
strBuilder.append(
"<tr>"
);
strBuilder.append(
"<td>"
);
strBuilder.append(data.get(
"UserName"
).toString());
strBuilder.append(
"</td>"
);
strBuilder.append(
"<td>"
);
strBuilder.append(data.get(
"UserPhoneNumber"
).toString());
strBuilder.append(
"</td>"
);
strBuilder.append(
"</tr>"
);
}
}
strBuilder.append(
"</table></body></html>"
);
MailSenderInfo mailSendInfo = BuildMailSendInfo(
"第"
+ index +
"批手机号"
, strBuilder.toString());
SimpleMailSender.sendHtmlMail(mailSendInfo);
}
}
|
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
|
package
bruce.util.mail;
import
java.util.Date;
import
java.util.Properties;
import
javax.activation.CommandMap;
import
javax.activation.MailcapCommandMap;
import
javax.mail.Address;
import
javax.mail.BodyPart;
import
javax.mail.Message;
import
javax.mail.MessagingException;
import
javax.mail.Multipart;
import
javax.mail.Session;
import
javax.mail.Transport;
import
javax.mail.internet.InternetAddress;
import
javax.mail.internet.MimeBodyPart;
import
javax.mail.internet.MimeMessage;
import
javax.mail.internet.MimeMultipart;
public
class
SimpleMailSender {
/**
* 以文本格式发送邮件
*
* @param mailInfo
* 待发送的邮件的信息
*/
public
static
boolean
sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator =
null
;
Properties pro = mailInfo.getProperties();
if
(mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator =
new
MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session
.getInstance(pro, authenticator);
try
{
// 根据session创建一个邮件消息
Message mailMessage =
new
MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from =
new
InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to =
new
InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(
new
Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return
true
;
}
catch
(MessagingException ex) {
ex.printStackTrace();
}
return
false
;
}
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
*/
public
static
boolean
sendHtmlMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator =
null
;
Properties pro = mailInfo.getProperties();
// 如果需要身份认证,则创建一个密码验证器
if
(mailInfo.isValidate()) {
authenticator =
new
MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session
.getInstance(pro, authenticator);
try
{
// 根据session创建一个邮件消息
Message mailMessage =
new
MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from =
new
InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to =
new
InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(
new
Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart =
new
MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html =
new
MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(),
"text/html; charset=utf-8"
);
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap(
"text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"
);
mc.addMailcap(
"text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"
);
mc.addMailcap(
"text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"
);
mc.addMailcap(
"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"
);
mc.addMailcap(
"message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"
);
CommandMap.setDefaultCommandMap(mc);
Transport.send(mailMessage);
return
true
;
}
catch
(MessagingException ex) {
ex.printStackTrace();
}
return
false
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
private
static
MailSenderInfo BuildMailSendInfo(String mailSubject,
String mailContent) {
MailSenderInfo mailInfo =
new
MailSenderInfo();
mailInfo.setContent(mailContent);
mailInfo.setFromAddress(
"Bruce.L.Li@163.com"
);
mailInfo.setMailServerHost(
"smtp.163.com"
);
mailInfo.setMailServerPort(
"25"
);
mailInfo.setPassword(
"********"
);
mailInfo.setSubject(mailSubject);
mailInfo.setToAddress(
"******@qq.com"
);
mailInfo.setUserName(
"lilei1986XXXX@163.com"
);
mailInfo.setValidate(
true
);
return
mailInfo;
}
|
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
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
CLUtility
{
public
class
EmailHelper
{
public
static
void
SendEmail(
string
mailTitle,
string
mailContent,
params
string
[] mailToAddress)
{
System.Net.Mail.SmtpClient client =
new
System.Net.Mail.SmtpClient();
client.Host =
"smtp.ym.163.com"
;
//使用163的SMTP服务器发送邮件
client.UseDefaultCredentials =
true
;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials =
new
System.Net.NetworkCredential(
"Bruce.XXX.XX@163.com"
,
"*******"
);
System.Net.Mail.MailMessage Message =
new
System.Net.Mail.MailMessage();
Message.From =
new
System.Net.Mail.MailAddress(
"lilei1986abc@163.com"
);
foreach
(
var
address
in
mailToAddress)
{
Message.To.Add(address);
}
Message.Subject = mailTitle;
Message.Body = mailContent;
Message.SubjectEncoding = System.Text.Encoding.UTF8;
Message.BodyEncoding = System.Text.Encoding.UTF8;
Message.Priority = System.Net.Mail.MailPriority.High;
Message.IsBodyHtml =
true
;
client.Send(Message);
}
}
}
|