原文地址:http://www.xue5.com/itedu/10813.html

Mail
1
using System;
2
using System.Text;
3
using System.IO;
4
using System.Net;
5
using System.Net.Sockets;
6
using System.Collections;
7
8
namespace SkyDev.Web.Mail
9

{
10
public enum MailFormat
{Text,HTML};
11
public enum MailPriority
{Low=1,Normal=3,High=5};
12
13
Class mailAttachments#region Class mailAttachments
14
public class MailAttachments
15
{
16
private const int MaxAttachmentNum=10;
17
private IList _Attachments;
18
19
public MailAttachments()
20
{
21
_Attachments=new ArrayList();
22
}
23
24
public string this[int index]
25
{
26
get
{ return (string)_Attachments[index];}
27
}
28
/**//// <summary>
29
/// 添加邮件附件
30
/// </summary>
31
/// <param name="FilePath">附件的绝对路径</param>
32
public void Add(params string[] filePath)
33
{
34
if(filePath==null)
35
{
36
throw(new ArgumentNullException("非法的附件"));
37
}
38
else
39
{
40
for(int i=0;i<filePath.Length;i++)
41
{
42
Add(filePath[i]);
43
}
44
}
45
}
46
47
/**//// <summary>
48
/// 添加一个附件,当指定的附件不存在时,忽略该附件,不产生异常。
49
/// </summary>
50
/// <param name="filePath">附件的绝对路径</param>
51
public void Add(string filePath)
52
{
53
//当附件存在时才加入,否则忽略
54
if (System.IO.File.Exists(filePath))
55
{
56
if (_Attachments.Count<MaxAttachmentNum)
57
{
58
_Attachments.Add(filePath);
59
}
60
}
61
}
62
63
public void Clear()//清除所有附件
64
{
65
_Attachments.Clear();
66
}
67
68
public int Count//获取附件个数
69
{
70
get
{ return _Attachments.Count;}
71
}
72
73
}
74
#endregion//end Class mailAttachments
75
76
77
78
Class MailMessage#region Class MailMessage
79
/**//// <summary>
80
/// MailMessage 表示SMTP要发送的一封邮件的消息。
81
/// </summary>
82
public class MailMessage
83
{
84
private const int MaxRecipientNum=10;
85
public MailMessage()
86
{
87
_Recipients=new ArrayList();//收件人列表
88
_Attachments=new MailAttachments();//附件
89
_BodyFormat=MailFormat.Text;//缺省的邮件格式为Text
90
_Priority=MailPriority.Normal;
91
_Charset="GB2312";
92
}
93
94
/**//// <summary>
95
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
96
/// </summary>
97
public string Charset
98
{
99
get
{ return _Charset;}
100
set
{ _Charset=value;}
101
}
102
103
public string From
104
{
105
get
{ return _From;}
106
set
{ _From=value;}
107
}
108
109
public string FromName
110
{
111
get
{ return _FromName;}
112
set
{ _FromName=value;}
113
}
114
public string Body
115
{
116
get
{ return _Body;}
117
set
{ _Body=value;}
118
}
119
120
public string Subject
121
{
122
get
{ return _Subject;}
123
set
{ _Subject=value;}
124
}
125
126
public MailAttachments Attachments
127
{
128
get
{return _Attachments;}
129
set
{ _Attachments=value;}
130
}
131
132
public MailPriority Priority
133
{
134
get
{ return _Priority;}
135
set
{ _Priority=value;}
136
}
137
138
public IList Recipients
139
{
140
get
{ return _Recipients;}
141
}
142
/**//// <summary>
143
/// 增加一个收件人地址
144
/// </summary>
145
/// <param name="recipient">收件人的Email地址</param>
146
public void AddRecipients(string recipient)
147
{
148
//先检查邮件地址是否符合规范
149
if (_Recipients.Count<MaxRecipientNum)
150
{
151
_Recipients.Add(recipient);//增加到收件人列表
152
}
153
}
154
155
public void AddRecipients(params string[] recipient)
156
{
157
if (recipient==null)
158
{
159
throw (new ArgumentException("收件人不能为空."));
160
}
161
else
162
{
163
for (int i=0;i<recipient.Length;i++)
164
{
165
AddRecipients(recipient[i]);
166
}
167
}
168
}
169
170
public MailFormat BodyFormat
171
{
172
set
{ _BodyFormat=value;}
173
get
{ return _BodyFormat;}
174
}
175
176
private string _From;//发件人地址
177
private string _FromName;//发件人姓名
178
private IList _Recipients;//收件人
179
private MailAttachments _Attachments;//附件
180
private string _Body;//内容
181
private string _Subject;//主题
182
private MailFormat _BodyFormat;//邮件格式
183
private string _Charset="GB2312";//字符编码格式
184
private MailPriority _Priority;//邮件优先级
185
}
186
#endregion
187
188
189
Class SmtpMail#region Class SmtpMail
190
public class SmtpServerHelper
191
{
192
private string CRLF="\r\n";//回车换行
193
194
/**//// <summary>
195
/// 错误消息反馈
196
/// </summary>
197
private string errmsg;
198
199
/**//// <summary>
200
/// TcpClient对象,用于连接服务器
201
/// </summary>
202
private TcpClient tcpClient;
203
204
/**//// <summary>
205
/// NetworkStream对象
206
/// </summary>
207
private NetworkStream networkStream;
208
209
/**//// <summary>
210
/// 服务器交互记录
211
/// </summary>
212
private string logs="";
213
214
/**//// <summary>
215
/// SMTP错误代码哈希表
216
/// </summary>
217
private Hashtable ErrCodeHT = new Hashtable();
218
219
/**//// <summary>
220
/// SMTP正确代码哈希表
221
/// </summary>
222
private Hashtable RightCodeHT = new Hashtable();
223
224
public SmtpServerHelper()
225
{
226
SMTPCodeAdd();//初始化SMTPCode
227
}
228
229
~SmtpServerHelper()
230
{
231
networkStream.Close();
232
tcpClient.Close();
233
}
234
235
/**//// <summary>
236
/// 将字符串编码为Base64字符串
237
/// </summary>
238
/// <param name="str">要编码的字符串</param>
239
private string Base64Encode(string str)
240
{
241
byte[] barray;
242
barray=Encoding.Default.GetBytes(str);
243
return Convert.ToBase64String(barray);
244
}
245
246
/**//// <summary>
247
/// 将Base64字符串解码为普通字符串
248
/// </summary>
249
/// <param name="str">要解码的字符串</param>
250
private string Base64Decode(string str)
251
{
252
byte[] barray;
253
barray=Convert.FromBase64String(str);
254
return Encoding.Default.GetString(barray);
255
}
256
257
/**//// <summary>
258
/// 得到上传附件的文件流
259
/// </summary>
260
/// <param name="FilePath">附件的绝对路径</param>
261
private string GetStream(string FilePath)
262
{
263
//建立文件流对象
264
System.IO.FileStream FileStr=new System.IO.FileStream(FilePath,System.IO.FileMode.Open);
265
byte[] by=new byte[System.Convert.ToInt32(FileStr.Length)];
266
FileStr.Read(by,0,by.Length);
267
FileStr.Close();
268
return(System.Convert.ToBase64String(by));
269
}
270
271
/**//// <summary>
272
/// SMTP回应代码哈希表
273
/// </summary>
274
private void SMTPCodeAdd()
275
{
276
//[RFC 821 4.2.1.]
277
/**//*
278
4.2.2. NUMERIC ORDER LIST OF REPLY CODES
279
280
211 System status, or system help reply
281
214 Help message
282
[Information on how to use the receiver or the meaning of a
283
particular non-standard command; this reply is useful only
284
to the human user]
285
220 <domain> Service ready
286
221 <domain> Service closing transmission channel
287
250 Requested mail action okay, completed
288
251 User not local; will forward to <forward-path>
289
290
354 Start mail input; end with <CRLF>.<CRLF>
291
292
421 <domain> Service not available,
293
closing transmission channel
294
[This may be a reply to any command if the service knows it
295
must shut down]
296
450 Requested mail action not taken: mailbox unavailable
297
[E.g., mailbox busy]
298
451 Requested action aborted: local error in processing
299
452 Requested action not taken: insufficient system storage
300
301
500 Syntax error, command unrecognized
302
[This may include errors such as command line too long]
303
501 Syntax error in parameters or arguments
304
502 Command not implemented
305
503 Bad sequence of commands
306
504 Command parameter not implemented
307
550 Requested action not taken: mailbox unavailable
308
[E.g., mailbox not found, no access]
309
551 User not local; please try <forward-path>
310
552 Requested mail action aborted: exceeded storage allocation
311
553 Requested action not taken: mailbox name not allowed
312
[E.g., mailbox syntax incorrect]
313
554 Transaction failed
314
315
*/
316
317
ErrCodeHT.Add("421","服务未就绪,关闭传输信道");
318
ErrCodeHT.Add("432","需要一个密码转换");
319
ErrCodeHT.Add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
320
ErrCodeHT.Add("451","放弃要求的操作;处理过程中出错");
321
ErrCodeHT.Add("452","系统存储不足,要求的操作未执行");
322
ErrCodeHT.Add("454","临时认证失败");
323
ErrCodeHT.Add("500","邮箱地址错误");
324
ErrCodeHT.Add("501","参数格式错误");
325
ErrCodeHT.Add("502","命令不可实现");
326
ErrCodeHT.Add("503","服务器需要SMTP验证");
327
ErrCodeHT.Add("504","命令参数不可实现");
328
ErrCodeHT.Add("530","需要认证");
329
ErrCodeHT.Add("534","认证机制过于简单");
330
ErrCodeHT.Add("538","当前请求的认证机制需要加密");
331
ErrCodeHT.Add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
332
ErrCodeHT.Add("551","用户非本地,请尝试<forward-path>");
333
ErrCodeHT.Add("552","过量的存储分配,要求的操作未执行");
334
ErrCodeHT.Add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
335
ErrCodeHT.Add("554","传输失败");
336
337
338
/**//*
339
211 System status, or system help reply
340
214 Help message
341
[Information on how to use the receiver or the meaning of a
342
particular non-standard command; this reply is useful only
343
to the human user]
344
220 <domain> Service ready
345
221 <domain> Service closing transmission channel
346
250 Requested mail action okay, completed
347
251 User not local; will forward to <forward-path>
348
349
354 Start mail input; end with <CRLF>.<CRLF>
350
*/
351
352
RightCodeHT.Add("220","服务就绪");
353
RightCodeHT.Add("221","服务关闭传输信道");
354
RightCodeHT.Add("235","验证成功");
355
RightCodeHT.Add("250","要求的邮件操作完成");
356
RightCodeHT.Add("251","非本地用户,将转发向<forward-path>");
357
RightCodeHT.Add("334","服务器响应验证Base64字符串");
358
RightCodeHT.Add("354","开始邮件输入,以<CRLF>.<CRLF>结束");
359
360
}
361
362
/**//// <summary>
363
/// 发送SMTP命令
364
/// </summary>
365
private bool SendCommand(string str)
366
{
367
byte[]WriteBuffer;
368
if(str==null||str.Trim()==String.Empty)
369
{
370
return true;
371
}
372
logs+=str;
373
WriteBuffer = Encoding.Default.GetBytes(str);
374
try
375
{
376
networkStream.Write(WriteBuffer,0,WriteBuffer.Length);
377
}
378
catch
379
{
380
errmsg="网络连接错误";
381
return false;
382
}
383
return true;
384
}
385
386
/**//// <summary>
387
/// 接收SMTP服务器回应
388
/// </summary>
389
private string RecvResponse()
390
{
391
int StreamSize;
392
string Returnvalue = String.Empty;
393
byte[] ReadBuffer = new byte[1024] ;
394
try
395
{
396
StreamSize=networkStream.Read(ReadBuffer,0,ReadBuffer.Length);
397
}
398
catch
399
{
400
errmsg="网络连接错误";
401
return "false";
402
}
403
404
if (StreamSize==0)
405
{
406
return Returnvalue ;
407
}
408
else
409
{
410
Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
411
logs+=Returnvalue+this.CRLF;
412
return Returnvalue;
413
}
414
}
415
416
/**//// <summary>
417
/// 与服务器交互,发送一条命令并接收回应。
418
/// </summary>
419
/// <param name="str">一个要发送的命令</param>
420
/// <param name="errstr">如果错误,要反馈的信息</param>
421
private bool Dialog(string str,string errstr)
422
{
423
if(str==null||str.Trim()==string.Empty)
424
{
425
return true;
426
}
427
if(SendCommand(str))
428
{
429
string RR=RecvResponse();
430
if(RR=="false")
431
{
432
return false;
433
}
434
//检查返回的代码,根据[RFC 821]返回代码为3位数字代码如220
435
string RRCode=RR.Substring(0,3);
436
if(RightCodeHT[RRCode]!=null)
437
{
438
return true;
439
}
440
else
441
{
442
if(ErrCodeHT[RRCode]!=null)
443
{
444
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
445
errmsg+=CRLF;
446
}
447
else
448
{
449
errmsg+=RR;
450
}
451
errmsg+=errstr;
452
return false;
453
}
454
}
455
else
456
{
457
return false;
458
}
459
}
460
461
462
/**//// <summary>
463
/// 与服务器交互,发送一组命令并接收回应。
464
/// </summary>
465
466
private bool Dialog(string[] str,string errstr)
467
{
468
for(int i=0;i<str.Length;i++)
469
{
470
if(!Dialog(str[i],""))
471
{
472
errmsg+=CRLF;
473
errmsg+=errstr;
474
return false;
475
}
476
}
477
478
return true;
479
}
480
481
482
//连接服务器
483
private bool Connect(string smtpServer,int port)
484
{
485
//创建Tcp连接
486
try
487
{
488
tcpClient=new TcpClient(smtpServer,port);
489
}
490
catch(Exception e)
491
{
492
errmsg=e.ToString();
493
return false;
494
}
495
networkStream=tcpClient.GetStream();
496
497
//验证网络连接是否正确
498
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
499
{
500
errmsg="网络连接失败";
501
return false;
502
}
503
return true;
504
}
505
506
private string GetPriorityString(MailPriority mailPriority)
507
{
508
string priority="Normal";
509
if (mailPriority==MailPriority.Low)
510
{
511
priority="Low";
512
}
513
else if (mailPriority==MailPriority.High)
514
{
515
priority="High";
516
}
517
return priority;
518
}
519
520
/**//// <summary>
521
/// 发送电子邮件,SMTP服务器不需要身份验证
522
/// </summary>
523
/// <param name="smtpServer"></param>
524
/// <param name="port"></param>
525
/// <param name="mailMessage"></param>
526
/// <returns></returns>
527
public bool SendEmail(string smtpServer,int port,MailMessage mailMessage)
528
{
529
return SendEmail(smtpServer,port,false,"","",mailMessage);
530
}
531
532
/**//// <summary>
533
/// 发送电子邮件,SMTP服务器需要身份验证
534
/// </summary>
535
/// <param name="smtpServer"></param>
536
/// <param name="port"></param>
537
/// <param name="username"></param>
538
/// <param name="password"></param>
539
/// <param name="mailMessage"></param>
540
/// <returns></returns>
541
public bool SendEmail(string smtpServer,int port,string username,string password,MailMessage mailMessage)
542
{
543
return SendEmail(smtpServer,port,false,username,password,mailMessage);
544
}
545
546
private bool SendEmail(string smtpServer,int port,bool ESmtp,string username,string password,MailMessage mailMessage)
547
{
548
if (Connect(smtpServer,port)==false)//测试连接服务器是否成功
549
return false;
550
551
string priority=GetPriorityString(mailMessage.Priority);
552
bool Html=(mailMessage.BodyFormat==MailFormat.HTML);
553
554
string[] SendBuffer;
555
string SendBufferstr;
556
557
//进行SMTP验证,现在大部分SMTP服务器都要认证
558
if(ESmtp)
559
{
560
SendBuffer=new String[4];
561
SendBuffer[0]="EHLO " + smtpServer + CRLF;
562
SendBuffer[1]="AUTH LOGIN" + CRLF;
563
SendBuffer[2]=Base64Encode(username) + CRLF;
564
SendBuffer[3]=Base64Encode(password) + CRLF;
565
if(!Dialog(SendBuffer,"SMTP服务器验证失败,请核对用户名和密码。"))
566
return false;
567
}
568
else
569
{//不需要身份认证
570
SendBufferstr="HELO " + smtpServer + CRLF;
571
if(!Dialog(SendBufferstr,""))
572
return false;
573
}
574
575
//发件人地址
576
SendBufferstr="MAIL FROM:<" + mailMessage.From + ">" + CRLF;
577
if(!Dialog(SendBufferstr,"发件人地址错误,或不能为空"))
578
return false;
579
580
//收件人地址
581
SendBuffer=new string[mailMessage.Recipients.Count];
582
for(int i=0;i<mailMessage.Recipients.Count;i++)
583
{
584
SendBuffer[i]="RCPT TO:<" +(string)mailMessage.Recipients[i] +">" + CRLF;
585
}
586
if(!Dialog(SendBuffer,"收件人地址有误"))
587
return false;
588
589
/**//*
590
SendBuffer=new string[10];
591
for(int i=0;i<RecipientBCC.Count;i++)
592
{
593
594
SendBuffer[i]="RCPT TO:<" + RecipientBCC[i].ToString() +">" + CRLF;
595
596
}
597
598
if(!Dialog(SendBuffer,"密件收件人地址有误"))
599
return false;
600
*/
601
602
SendBufferstr="DATA" + CRLF;
603
if(!Dialog(SendBufferstr,""))
604
return false;
605
606
//发件人姓名
607
SendBufferstr="From:" + mailMessage.FromName + "<" +mailMessage.From +">" +CRLF;
608
609
//if(ReplyTo.Trim()!="")
610
//{
611
// SendBufferstr+="Reply-To: " + ReplyTo + CRLF;
612
//}
613
614
//SendBufferstr+="To:" + ToName + "<" + Recipient[0] +">" +CRLF;
615
//至少要有一个收件人
616
if (mailMessage.Recipients.Count==0)
617
{
618
return false;
619
}
620
else
621
{
622
SendBufferstr += "To:=?"+mailMessage.Charset.ToUpper()+"?B?"+
623
Base64Encode((string)mailMessage.Recipients[0])+"?="+"<"+(string)mailMessage.Recipients[0]+">"+CRLF;
624
}
625
626
//SendBufferstr+="CC:";
627
//for(int i=0;i<Recipient.Count;i++)
628
//{
629
// SendBufferstr+=Recipient[i].ToString() + "<" + Recipient[i].ToString() +">,";
630
//}
631
//SendBufferstr+=CRLF;
632
633
SendBufferstr+=
634
((mailMessage.Subject==String.Empty || mailMessage.Subject==null)?"Subject:":((mailMessage.Charset=="")?("Subject:" +
635
mailMessage.Subject):("Subject:" + "=?" + mailMessage.Charset.ToUpper() + "?B?" +
636
Base64Encode(mailMessage.Subject) +"?="))) + CRLF;
637
SendBufferstr+="X-Priority:" + priority + CRLF;
638
SendBufferstr+="X-MSMail-Priority:" + priority + CRLF;
639
SendBufferstr+="Importance:" + priority + CRLF;
640
SendBufferstr+="X-Mailer: Lion.Web.Mail.SmtpMail Pubclass [cn]" + CRLF;
641
SendBufferstr+="MIME-Version: 1.0" + CRLF;
642
if(mailMessage.Attachments.Count!=0)
643
{
644
SendBufferstr+="Content-Type: multipart/mixed;" + CRLF;
645
SendBufferstr += " boundary=\"====="+
646
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====\""+CRLF+CRLF;
647
}
648
649
if(Html)
650
{
651
if(mailMessage.Attachments.Count==0)
652
{
653
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
654
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\""+CRLF+CRLF;
655
SendBufferstr += "This is a multi-part message in MIME format."+CRLF+CRLF;
656
}
657
else
658
{
659
SendBufferstr +="This is a multi-part message in MIME format."+CRLF+CRLF;
660
SendBufferstr += "--=====001_Dragon520636771063_====="+CRLF;
661
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
662
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\""+CRLF+CRLF;
663
}
664
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
665
SendBufferstr += "Content-Type: text/plain;"+ CRLF;
666
SendBufferstr += ((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
667
668
mailMessage.Charset.ToLower() + "\"")) + CRLF;
669
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
670
SendBufferstr+= Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + CRLF + CRLF;
671
672
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
673
674
675
SendBufferstr+="Content-Type: text/html;" + CRLF;
676
SendBufferstr+=((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
677
mailMessage.Charset.ToLower() + "\"")) + CRLF;
678
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
679
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF + CRLF;
680
SendBufferstr += "--=====003_Dragon520636771063_=====--"+CRLF;
681
}
682
else
683
{
684
if(mailMessage.Attachments.Count!=0)
685
{
686
SendBufferstr += "--=====001_Dragon303406132050_====="+CRLF;
687
}
688
SendBufferstr+="Content-Type: text/plain;" + CRLF;
689
SendBufferstr+=((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
690
mailMessage.Charset.ToLower() + "\"")) + CRLF;
691
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
692
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF;
693
}
694
695
//SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF;
696
697
if(mailMessage.Attachments.Count!=0)
698
{
699
for(int i=0;i<mailMessage.Attachments.Count;i++)
700
{
701
string filepath = (string)mailMessage.Attachments[i];
702
SendBufferstr += "--====="+
703
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_") +"====="+CRLF;
704
//SendBufferstr += "Content-Type: application/octet-stream"+CRLF;
705
SendBufferstr += "Content-Type: text/plain;"+CRLF;
706
SendBufferstr += " name=\"=?"+mailMessage.Charset.ToUpper()+"?B?"+
707
Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+CRLF;
708
SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF;
709
SendBufferstr += "Content-Disposition: attachment;"+CRLF;
710
SendBufferstr += " filename=\"=?"+mailMessage.Charset.ToUpper()+"?B?"+
711
Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+CRLF+CRLF;
712
SendBufferstr += GetStream(filepath)+CRLF+CRLF;
713
}
714
SendBufferstr += "--====="+
715
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====--"+CRLF+CRLF;
716
}
717
718
SendBufferstr += CRLF + "." + CRLF;//内容结束
719
720
if(!Dialog(SendBufferstr,"错误信件信息"))
721
return false;
722
723
SendBufferstr="QUIT" + CRLF;
724
if(!Dialog(SendBufferstr,"断开连接时错误"))
725
return false;
726
727
networkStream.Close();
728
tcpClient.Close();
729
return true;
730
}
731
}
732
733
734
735
public class SmtpMail
736
{
737
private static string _SmtpServer;
738
739
/**//// <summary>
740
/// 格式:SmtpAccount:Password@SmtpServerAddress<br>
741
/// 或者:SmtpServerAddress<br>
742
/// <code>
743
/// SmtpMail.SmtpServer="user:12345678@smtp.126.com";
744
/// //或者:
745
/// SmtpMail.SmtpServer="smtp.126.com";
746
/// 或者:
747
/// SmtpMail.SmtpServer=SmtpServerHelper.GetSmtpServer("user","12345678","smtp.126.com");
748
/// </code>
749
/// </summary>
750
public static string SmtpServer
751
{
752
set
{ _SmtpServer=value;}
753
get
{ return _SmtpServer;}
754
}
755
756
public static bool Send(MailMessage mailMessage,string username,string password)
757
{
758
SmtpServerHelper helper=new SmtpServerHelper();
759
return helper.SendEmail(_SmtpServer,25,username,password,mailMessage);
760
}
761
762
}
763
764
#endregion
765
}
766

调用
1
using System;
2
using NUnit.Framework;
3
4
5
namespace SkyDev.Web.Mail
6

{
7
/**//// <summary>
8
/// Test 的摘要说明。
9
/// </summary>
10
[TestFixture]
11
public class TestSmtpMail
12
{
13
//安装测试用例,完成初始化操作
14
[SetUp]
15
public void SetUp()
16
{
17
}
18
19
//测试结束完成清理操作
20
[TearDown]
21
public void TearDown()
22
{
23
24
}
25
26
[Test]
27
public void TestMailAttachments()
28
{
29
SkyDev.Web.Mail.MailAttachments attachments=new MailAttachments();
30
Assert.AreEqual(0,attachments.Count,"初始化MailAttachments");
31
attachments.Add("c:\\autoexec.bat");
32
Assert.AreEqual(1,attachments.Count,"增加附件(附件确实存在)");
33
attachments.Add("c:\\autoexec.dat.txt");
34
Assert.AreEqual(1,attachments.Count,"增加附件(附件不存在)");
35
attachments.Clear();
36
Assert.AreEqual(0,attachments.Count,"清除附件");
37
}
38
39
[Test]
40
public void TestMailMessage()
41
{
42
MailMessage message=new MailMessage();
43
Assert.AreEqual(0,message.Attachments.Count,"初始化MailAttachments");
44
Assert.AreEqual(MailFormat.Text,message.BodyFormat,"邮件格式");
45
Assert.AreEqual("GB2312",message.Charset,"缺省的字符集");
46
}
47
48
[Test]
49
public void TestSendMail()
50
{
51
SmtpMail.SmtpServer="smtp.126.com";
52
MailMessage mail=new MailMessage();
53
mail.From="qs1976@126.com";
54
mail.FromName="曾青松";
55
mail.AddRecipients("qs1976@126.com");
56
mail.Subject="主题:测试邮件";
57
mail.BodyFormat=MailFormat.Text;
58
mail.Body="测试的内容.";
59
mail.Attachments.Add("c:\\test.txt");
60
SmtpMail.Send(mail,"","");//请填写自己的测试邮件帐号
61
}
62
}
63