原文地址:http://www.xue5.com/itedu/10813.html
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
namespace SkyDev.Web.Mail
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public enum MailFormat
{Text,HTML};
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public enum MailPriority
{Low=1,Normal=3,High=5};
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Class mailAttachments#region Class mailAttachments
14
public class MailAttachments
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
private const int MaxAttachmentNum=10;
17
private IList _Attachments;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
public MailAttachments()
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
_Attachments=new ArrayList();
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
public string this[int index]
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (string)_Attachments[index];}
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
29
/// 添加邮件附件
30
/// </summary>
31
/// <param name="FilePath">附件的绝对路径</param>
32
public void Add(params string[] filePath)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
if(filePath==null)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
throw(new ArgumentNullException("非法的附件"));
37
}
38
else
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
for(int i=0;i<filePath.Length;i++)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
Add(filePath[i]);
43
}
44
}
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
48
/// 添加一个附件,当指定的附件不存在时,忽略该附件,不产生异常。
49
/// </summary>
50
/// <param name="filePath">附件的绝对路径</param>
51
public void Add(string filePath)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
//当附件存在时才加入,否则忽略
54
if (System.IO.File.Exists(filePath))
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
if (_Attachments.Count<MaxAttachmentNum)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
_Attachments.Add(filePath);
59
}
60
}
61
}
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
public void Clear()//清除所有附件
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
_Attachments.Clear();
66
}
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
public int Count//获取附件个数
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Attachments.Count;}
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
}
74
#endregion//end Class mailAttachments
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Class MailMessage#region Class MailMessage
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
80
/// MailMessage 表示SMTP要发送的一封邮件的消息。
81
/// </summary>
82
public class MailMessage
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
private const int MaxRecipientNum=10;
85
public MailMessage()
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
_Recipients=new ArrayList();//收件人列表
88
_Attachments=new MailAttachments();//附件
89
_BodyFormat=MailFormat.Text;//缺省的邮件格式为Text
90
_Priority=MailPriority.Normal;
91
_Charset="GB2312";
92
}
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
95
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
96
/// </summary>
97
public string Charset
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Charset;}
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Charset=value;}
101
}
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
public string From
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _From;}
106![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _From=value;}
107
}
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
public string FromName
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _FromName;}
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _FromName=value;}
113
}
114
public string Body
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Body;}
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Body=value;}
118
}
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
public string Subject
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Subject;}
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Subject=value;}
124
}
125![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
public MailAttachments Attachments
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return _Attachments;}
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Attachments=value;}
130
}
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
public MailPriority Priority
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Priority;}
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Priority=value;}
136
}
137![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
public IList Recipients
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Recipients;}
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
143
/// 增加一个收件人地址
144
/// </summary>
145
/// <param name="recipient">收件人的Email地址</param>
146
public void AddRecipients(string recipient)
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
//先检查邮件地址是否符合规范
149
if (_Recipients.Count<MaxRecipientNum)
150![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
_Recipients.Add(recipient);//增加到收件人列表
152
}
153
}
154![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
public void AddRecipients(params string[] recipient)
156![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
157
if (recipient==null)
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
throw (new ArgumentException("收件人不能为空."));
160
}
161
else
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
for (int i=0;i<recipient.Length;i++)
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
AddRecipients(recipient[i]);
166
}
167
}
168
}
169![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
public MailFormat BodyFormat
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
172![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _BodyFormat=value;}
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _BodyFormat;}
174
}
175![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
188![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
189![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Class SmtpMail#region Class SmtpMail
190
public class SmtpServerHelper
191![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
192
private string CRLF="\r\n";//回车换行
193![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
195
/// 错误消息反馈
196
/// </summary>
197
private string errmsg;
198![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
200
/// TcpClient对象,用于连接服务器
201
/// </summary>
202
private TcpClient tcpClient;
203![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
204![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
205
/// NetworkStream对象
206
/// </summary>
207
private NetworkStream networkStream;
208![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
209![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
210
/// 服务器交互记录
211
/// </summary>
212
private string logs="";
213![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
215
/// SMTP错误代码哈希表
216
/// </summary>
217
private Hashtable ErrCodeHT = new Hashtable();
218![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
219![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
220
/// SMTP正确代码哈希表
221
/// </summary>
222
private Hashtable RightCodeHT = new Hashtable();
223![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
224
public SmtpServerHelper()
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226
SMTPCodeAdd();//初始化SMTPCode
227
}
228![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
229
~SmtpServerHelper()
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
231
networkStream.Close();
232
tcpClient.Close();
233
}
234![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
236
/// 将字符串编码为Base64字符串
237
/// </summary>
238
/// <param name="str">要编码的字符串</param>
239
private string Base64Encode(string str)
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241
byte[] barray;
242
barray=Encoding.Default.GetBytes(str);
243
return Convert.ToBase64String(barray);
244
}
245![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
247
/// 将Base64字符串解码为普通字符串
248
/// </summary>
249
/// <param name="str">要解码的字符串</param>
250
private string Base64Decode(string str)
251![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
252
byte[] barray;
253
barray=Convert.FromBase64String(str);
254
return Encoding.Default.GetString(barray);
255
}
256![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
257![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
258
/// 得到上传附件的文件流
259
/// </summary>
260
/// <param name="FilePath">附件的绝对路径</param>
261
private string GetStream(string FilePath)
262![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
271![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
272
/// SMTP回应代码哈希表
273
/// </summary>
274
private void SMTPCodeAdd()
275![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
276
//[RFC 821 4.2.1.]
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
278
4.2.2. NUMERIC ORDER LIST OF REPLY CODES
279![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
338![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
360
}
361![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
362![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
363
/// 发送SMTP命令
364
/// </summary>
365
private bool SendCommand(string str)
366![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
367
byte[]WriteBuffer;
368
if(str==null||str.Trim()==String.Empty)
369![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
370
return true;
371
}
372
logs+=str;
373
WriteBuffer = Encoding.Default.GetBytes(str);
374
try
375![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
376
networkStream.Write(WriteBuffer,0,WriteBuffer.Length);
377
}
378
catch
379![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
380
errmsg="网络连接错误";
381
return false;
382
}
383
return true;
384
}
385![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
386![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
387
/// 接收SMTP服务器回应
388
/// </summary>
389
private string RecvResponse()
390![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
391
int StreamSize;
392
string Returnvalue = String.Empty;
393
byte[] ReadBuffer = new byte[1024] ;
394
try
395![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
396
StreamSize=networkStream.Read(ReadBuffer,0,ReadBuffer.Length);
397
}
398
catch
399![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
400
errmsg="网络连接错误";
401
return "false";
402
}
403![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
404
if (StreamSize==0)
405![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
406
return Returnvalue ;
407
}
408
else
409![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
410
Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
411
logs+=Returnvalue+this.CRLF;
412
return Returnvalue;
413
}
414
}
415![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
416![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
417
/// 与服务器交互,发送一条命令并接收回应。
418
/// </summary>
419
/// <param name="str">一个要发送的命令</param>
420
/// <param name="errstr">如果错误,要反馈的信息</param>
421
private bool Dialog(string str,string errstr)
422![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
423
if(str==null||str.Trim()==string.Empty)
424![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
425
return true;
426
}
427
if(SendCommand(str))
428![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
429
string RR=RecvResponse();
430
if(RR=="false")
431![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
432
return false;
433
}
434
//检查返回的代码,根据[RFC 821]返回代码为3位数字代码如220
435
string RRCode=RR.Substring(0,3);
436
if(RightCodeHT[RRCode]!=null)
437![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
438
return true;
439
}
440
else
441![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
442
if(ErrCodeHT[RRCode]!=null)
443![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
444
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
445
errmsg+=CRLF;
446
}
447
else
448![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
449
errmsg+=RR;
450
}
451
errmsg+=errstr;
452
return false;
453
}
454
}
455
else
456![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
457
return false;
458
}
459
}
460![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
461![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
462![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
463
/// 与服务器交互,发送一组命令并接收回应。
464
/// </summary>
465![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
466
private bool Dialog(string[] str,string errstr)
467![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
468
for(int i=0;i<str.Length;i++)
469![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
470
if(!Dialog(str[i],""))
471![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
472
errmsg+=CRLF;
473
errmsg+=errstr;
474
return false;
475
}
476
}
477![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
478
return true;
479
}
480![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
481![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
482
//连接服务器
483
private bool Connect(string smtpServer,int port)
484![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
485
//创建Tcp连接
486
try
487![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
488
tcpClient=new TcpClient(smtpServer,port);
489
}
490
catch(Exception e)
491![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
492
errmsg=e.ToString();
493
return false;
494
}
495
networkStream=tcpClient.GetStream();
496![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
497
//验证网络连接是否正确
498
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
499![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
500
errmsg="网络连接失败";
501
return false;
502
}
503
return true;
504
}
505![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
506
private string GetPriorityString(MailPriority mailPriority)
507![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
508
string priority="Normal";
509
if (mailPriority==MailPriority.Low)
510![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
511
priority="Low";
512
}
513
else if (mailPriority==MailPriority.High)
514![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
515
priority="High";
516
}
517
return priority;
518
}
519![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
520![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
529
return SendEmail(smtpServer,port,false,"","",mailMessage);
530
}
531![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
532![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
543
return SendEmail(smtpServer,port,false,username,password,mailMessage);
544
}
545![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
546
private bool SendEmail(string smtpServer,int port,bool ESmtp,string username,string password,MailMessage mailMessage)
547![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
548
if (Connect(smtpServer,port)==false)//测试连接服务器是否成功
549
return false;
550![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
551
string priority=GetPriorityString(mailMessage.Priority);
552
bool Html=(mailMessage.BodyFormat==MailFormat.HTML);
553
554
string[] SendBuffer;
555
string SendBufferstr;
556![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
557
//进行SMTP验证,现在大部分SMTP服务器都要认证
558
if(ESmtp)
559![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//不需要身份认证
570
SendBufferstr="HELO " + smtpServer + CRLF;
571
if(!Dialog(SendBufferstr,""))
572
return false;
573
}
574![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
575
//发件人地址
576
SendBufferstr="MAIL FROM:<" + mailMessage.From + ">" + CRLF;
577
if(!Dialog(SendBufferstr,"发件人地址错误,或不能为空"))
578
return false;
579![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
580
//收件人地址
581
SendBuffer=new string[mailMessage.Recipients.Count];
582
for(int i=0;i<mailMessage.Recipients.Count;i++)
583![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
584
SendBuffer[i]="RCPT TO:<" +(string)mailMessage.Recipients[i] +">" + CRLF;
585
}
586
if(!Dialog(SendBuffer,"收件人地址有误"))
587
return false;
588![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
589![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
590
SendBuffer=new string[10];
591
for(int i=0;i<RecipientBCC.Count;i++)
592
{
593![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
594
SendBuffer[i]="RCPT TO:<" + RecipientBCC[i].ToString() +">" + CRLF;
595![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
596
}
597![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
598
if(!Dialog(SendBuffer,"密件收件人地址有误"))
599
return false;
600
*/
601![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
602
SendBufferstr="DATA" + CRLF;
603
if(!Dialog(SendBufferstr,""))
604
return false;
605![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
606
//发件人姓名
607
SendBufferstr="From:" + mailMessage.FromName + "<" +mailMessage.From +">" +CRLF;
608![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
609
//if(ReplyTo.Trim()!="")
610
//{
611
// SendBufferstr+="Reply-To: " + ReplyTo + CRLF;
612
//}
613![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
614
//SendBufferstr+="To:" + ToName + "<" + Recipient[0] +">" +CRLF;
615
//至少要有一个收件人
616
if (mailMessage.Recipients.Count==0)
617![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
618
return false;
619
}
620
else
621![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
644
SendBufferstr+="Content-Type: multipart/mixed;" + CRLF;
645
SendBufferstr += " boundary=\"====="+
646
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====\""+CRLF+CRLF;
647
}
648![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
649
if(Html)
650![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
651
if(mailMessage.Attachments.Count==0)
652![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
668
mailMessage.Charset.ToLower() + "\"")) + CRLF;
669
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
670
SendBufferstr+= Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + CRLF + CRLF;
671![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
672
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
673![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
674![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
684
if(mailMessage.Attachments.Count!=0)
685![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
697
if(mailMessage.Attachments.Count!=0)
698![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
699
for(int i=0;i<mailMessage.Attachments.Count;i++)
700![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
720
if(!Dialog(SendBufferstr,"错误信件信息"))
721
return false;
722![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
723
SendBufferstr="QUIT" + CRLF;
724
if(!Dialog(SendBufferstr,"断开连接时错误"))
725
return false;
726![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
727
networkStream.Close();
728
tcpClient.Close();
729
return true;
730
}
731
}
732![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
733
734![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
735
public class SmtpMail
736![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
737
private static string _SmtpServer;
738![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
739![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
752![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _SmtpServer=value;}
753![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _SmtpServer;}
754
}
755![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
756
public static bool Send(MailMessage mailMessage,string username,string password)
757![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
758
SmtpServerHelper helper=new SmtpServerHelper();
759
return helper.SendEmail(_SmtpServer,25,username,password,mailMessage);
760
}
761![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
762
}
763![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
764
#endregion
765
}
766![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
调用
1
using System;
2
using NUnit.Framework;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace SkyDev.Web.Mail
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
8
/// Test 的摘要说明。
9
/// </summary>
10
[TestFixture]
11
public class TestSmtpMail
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
//安装测试用例,完成初始化操作
14
[SetUp]
15
public void SetUp()
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
//测试结束完成清理操作
20
[TearDown]
21
public void TearDown()
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
24
}
25
26
[Test]
27
public void TestMailAttachments()
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
[Test]
40
public void TestMailMessage()
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
[Test]
49
public void TestSendMail()
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)