邮件收发

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
/// <summary>
/// 邮件收发组件
/// </summary>
public class MailHelper
{
/// <summary>
/// 邮件服务器地址lr
/// </summary>
private static string MailServer = Config.GetValue("MailHost");
/// <summary>
/// 用户名lr
/// </summary>
private static string MailUserName = Config.GetValue("MailUserName");
/// <summary>
/// 密码lr
/// </summary>
private static string MailPassword = Config.GetValue("MailPassword");
/// <summary>
/// 名称lr
/// </summary>
private static string MailName = Config.GetValue("MailName");

/// <summary>
/// 邮件服务器地址xcfl
/// </summary>
private static string MailServerN = Config.GetValue("host");
/// <summary>
/// 用户名xcfl
/// </summary>
private static string MailUserNameN = Config.GetValue("mailFrom");
/// <summary>
/// 密码xcfl
/// </summary>
private static string MailPasswordN = Config.GetValue("mailPwd");
/// <summary>
/// 名称xcfl
/// </summary>
private static string MailNameN = Config.GetValue("mailFrom");
/// <summary>
/// 同步发送邮件
/// </summary>
/// <param name="to">收件人邮箱地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <param name="encoding">编码</param>
/// <param name="isBodyHtml">是否Html</param>
/// <param name="enableSsl">是否SSL加密连接</param>
/// <returns>是否成功</returns>
public static bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
{
try
{
MailMessage message = new MailMessage();
// 接收人邮箱地址
message.To.Add(new MailAddress(to));
message.From = new MailAddress(MailUserName, MailName);
message.BodyEncoding = Encoding.GetEncoding(encoding);
message.Body = body;
//GB2312
message.SubjectEncoding = Encoding.GetEncoding(encoding);
message.Subject = subject;
message.IsBodyHtml = isBodyHtml;

SmtpClient smtpclient = new SmtpClient(MailServer, 25);
smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
//SSL连接
smtpclient.EnableSsl = enableSsl;
smtpclient.Send(message);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
/// <summary>
/// 同步发送邮件XCFL
/// </summary>
/// <param name="to">收件人邮箱地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <param name="encoding">编码</param>
/// <param name="isBodyHtml">是否Html</param>
/// <param name="enableSsl">是否SSL加密连接</param>
/// <returns>是否成功</returns>
public static bool SendMine(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
{
try
{
MailMessage message = new MailMessage();
// 接收人邮箱地址
message.To.Add(new MailAddress(to));
message.From = new MailAddress(MailUserNameN, MailNameN);
message.BodyEncoding = Encoding.GetEncoding(encoding);
message.Body = body;
//GB2312
message.SubjectEncoding = Encoding.GetEncoding(encoding);
message.Subject = subject;
message.IsBodyHtml = isBodyHtml;

SmtpClient smtpclient = new SmtpClient(MailServerN, 25);
smtpclient.Credentials = new System.Net.NetworkCredential(MailUserNameN, MailPasswordN);
//SSL连接
smtpclient.EnableSsl = enableSsl;
smtpclient.Send(message);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 异步发送邮件 独立线程
/// </summary>
/// <param name="to">邮件接收人</param>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="port">端口号</param>
/// <returns></returns>
public static void SendByThread(string to, string title, string body, int port = 25)
{
new Thread(new ThreadStart(delegate ()
{
try
{
SmtpClient smtp = new SmtpClient();
//邮箱的smtp地址
smtp.Host = MailServer;
//端口号
smtp.Port = port;
//构建发件人的身份凭据类
smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
//构建消息类
MailMessage objMailMessage = new MailMessage();
//设置优先级
objMailMessage.Priority = MailPriority.High;
//消息发送人
objMailMessage.From = new MailAddress(MailUserName, "提醒", System.Text.Encoding.UTF8);
//收件人
objMailMessage.To.Add(to);
//标题
objMailMessage.Subject = title.Trim();
//标题字符编码
objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
//正文
objMailMessage.Body = body.Trim();
objMailMessage.IsBodyHtml = true;
//内容字符编码
objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
//发送
smtp.Send(objMailMessage);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

})).Start();
}
/// <summary>
/// 发送
/// </summary>
/// <param name="account">配置</param>
/// <param name="mailModel">信息</param>
public static void Send(MailAccount account, MailModel mailModel)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(account.Account, account.AccountName);
//发件人
if (!string.IsNullOrEmpty(mailModel.To))
{
var ToArray = mailModel.To.Split(',');
foreach (var item in ToArray)
{
mailMessage.To.Add(new MailAddress(item));
}
}
//抄送人
if (!string.IsNullOrEmpty(mailModel.CC))
{
var CCArray = mailModel.CC.Split(',');
foreach (var item in CCArray)
{
mailMessage.CC.Add(new MailAddress(item));
}
}
//密送人
if (!string.IsNullOrEmpty(mailModel.Bcc))
{
var BccArray = mailModel.Bcc.Split(',');
foreach (var item in BccArray)
{
mailMessage.Bcc.Add(new MailAddress(item));
}
}
//附件
//var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/");
//foreach (MailFile item in mailModel.Attachment)
//{
// var attachment = new Attachment(filePath + item.FileId);
// attachment.Name = item.FileName;
// mailMessage.Attachments.Add(attachment);
//}
mailMessage.Subject = mailModel.Subject;
mailMessage.Body = mailModel.BodyText;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Normal;
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.BodyEncoding = Encoding.UTF8;
//不被当作垃圾邮件的关键代码--Begin
mailMessage.Headers.Add("X-Priority", "3");
mailMessage.Headers.Add("X-MSMail-Priority", "Normal");
mailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
mailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
mailMessage.Headers.Add("ReturnReceipt", "1");
//不被当作垃圾邮件的关键代码--End
using (SmtpClient smtpClient = new SmtpClient(account.SMTPHost, account.SMTPPort))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(account.Account, account.Password);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(mailMessage);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="account">配置</param>
/// <param name="UID">UID</param>
public static void Delete(MailAccount account, string UID)
{
try
{
using (POP3_Client pop3Client = new POP3_Client())
{
pop3Client.Connect(account.POP3Host, account.POP3Port, false);
pop3Client.Login(account.Account, account.Password);
if (pop3Client.Messages.Count > 0)
{
foreach (POP3_ClientMessage messages in pop3Client.Messages)
{
if (messages.UID == UID)
{
messages.MarkForDeletion();
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取
/// </summary>
/// <param name="account">配置</param>
/// <param name="receiveCount">已收邮件数、注意:如果已收邮件数和邮件数量一致则不获取</param>
/// <returns></returns>
public static List<MailModel> Get(MailAccount account, int receiveCount)
{
try
{
var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/");
var resultList = new List<MailModel>();
using (POP3_Client pop3Client = new POP3_Client())
{
pop3Client.Connect(account.POP3Host, account.POP3Port, account.Ssl);
pop3Client.Login(account.Account, account.Password);
var messages = pop3Client.Messages;
if (receiveCount == messages.Count)
{
return resultList;
}
for (int i = messages.Count - 1; receiveCount <= i; i--)
{
var messageItem = messages[i];
var messageHeader = Mail_Message.ParseFromByte(messageItem.MessageToByte());
resultList.Add(new MailModel()
{
UID = messageItem.UID,
To = messageHeader.From == null ? "" : messageHeader.From[0].Address,
ToName = messageHeader.From == null ? "" : messageHeader.From[0].DisplayName,
Subject = messageHeader.Subject,
BodyText = messageHeader.BodyHtmlText,
Attachment = GetFile(filePath, messageHeader.GetAttachments(true, true), messageItem.UID),
Date = messageHeader.Date,
});
}
}
return resultList;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 解析附件并且下载到本地目录
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="messageFile">附件对象</param>
/// <param name="UID"></param>
/// <returns></returns>
private static List<MailFile> GetFile(string filePath, MIME_Entity[] messageFile, string UID)
{
var resultList = new List<MailFile>();
foreach (MIME_Entity entity in messageFile)
{
var fileName = entity.ContentType.Param_Name;
var fileByte = (MIME_b_SinglepartBase)entity.Body;
var fileId = UID + "_" + fileName;
DirFileHelper.CreateFile(filePath + fileId, fileByte.Data);
var fileSize = DirFileHelper.GetFileSize(filePath + fileId);
resultList.Add(new MailFile
{
FileId = fileId,
FileName = fileName,
FileSize = DirFileHelper.ToFileSize(fileSize)
});
}
return resultList;
}
}

MailAccount

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
public class MailAccount
{
/// <summary>
/// POP3服务
/// </summary>
public string POP3Host { get; set; }
/// <summary>
/// POP3端口
/// </summary>
public int POP3Port { get; set; }
/// <summary>
/// SMTP服务
/// </summary>
public string SMTPHost { get; set; }
/// <summary>
/// SMTP端口
/// </summary>
public int SMTPPort { get; set; }
/// <summary>
/// 账户
/// </summary>
public string Account { get; set; }
/// <summary>
/// 账户名称
/// </summary>
public string AccountName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// SSL
/// </summary>
public bool Ssl { get; set; }
}

MailFile

1
2
3
4
5
6
7
8
public class MailFile
{
public string FileId { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public DateTime FileTime { get; set; }
public string FileState { get; set; }
}

MailModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MailModel
{
public string UID { get; set; }
public string To { get; set; }
public string ToName { get; set; }
public string CC { get; set; }
public string CCName { get; set; }
public string Bcc { get; set; }
public string BccName { get; set; }
public string Subject { get; set; }
public string BodyText { get; set; }
public List<MailFile> Attachment { get; set; }
public DateTime Date { get; set; }
}