代码示例如下:
1
(in C#)
2
using System.Web.Mail;
3
public void sendMail()
4
{
5
try
6
{
7
System.Web.Mail.MailMessage myMail=new MailMessage();
8
myMail.From = "myaccount@test.com";
9
myMail.To = "myaccount@test.com";
10
myMail.Subject = "MailTest";
11
myMail.Priority = MailPriority.Low;
12
myMail.BodyFormat = MailFormat.Text;
13
myMail.Body = "Test";
14
SmtpMail.SmtpServer="smarthost"; //your smtp server here
15
16
SmtpMail.Send(myMail);
17
}
18
catch(Exception e)
19
{
20
throw e;
21
}
22
}
您可以在Send函数的参数MailMessage对象中设置邮件的相关属性,如优先级、附件等等。除了以MailMessage对象为参数(如上述代码),Send函数还可以简单的直接以邮件的4个主要信息(from,to,subject,messageText)作为字符串参数来调用。 
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

第二、使用CDO组件发送邮件
CDO是Collaboration Data Objects的简称,它是一组高层的COM对象集合,并经历了好几个版本的演化,现在在Windows2000和Exchange2000中使用的都是CDO2.0的版本(分别为cdosys.dll和cdoex.dll)。CDOSYS构建在SMTP协议和NNTP协议之上,并且作为Windows2000 Server的组件被安装,您可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。
CDO组件相对于先前介绍的SmtpMail对象功能更为丰富,并提供了一些SmtpMail类所没有提供的功能,如通过需要认证的SMTP服务器发送邮件等。
下面一段代码就展示了如何使用CDO组件通过需要认证的SMTP服务器发送邮件的过程:
1
(in C#)
2
public void CDOsendMail()
3
{
4
try
5
{
6
CDO.Message oMsg = new CDO.Message();
7
8
oMsg.From = "myaccount@test.com";
9
oMsg.To = "myaccount@test.com";
10
oMsg.Subject = "MailTest";
11
oMsg.HTMLBody = "Test ";
12
13
CDO.IConfiguration iConfg = oMsg.Configuration;
14
ADODB.Fields oFields = iConfg.Fields;
15
16
oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;
17
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "myaccount@test.com"; //sender mail
18
oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "myaccount@test.com"; //email account
19
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username";
20
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password";
21
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
22
//value=0 代表Anonymous验证方式(不需要验证)
23
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
24
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
25
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
26
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
27
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com";
28
oFields.Update();
29
oMsg.BodyPart.Charset="gb2312";
30
oMsg.HTMLBodyPart.Charset="gb2312";
31
32
oMsg.Send();
33
oMsg = null;
34
}
35
catch (Exception e)
36
{
37
throw e;
38
}
39
}
注意:由于Exchange2000的CDO组件cdoex.dll会更新原有的Windows2000的CDO组件cdosys.dll,所以如果您希望继续使用cdosys.dll,您必须先通过regsrv32.exe卸载掉cdoex.dll。
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

1
private void Page_Load(object sender, System.EventArgs e)
2
{
3
MailMessage mail = new MailMessage();
4
mail.To = "me@mycompany.com";
5
mail.From = "you@yourcompany.com";
6
mail.Subject = "this is a test email.";
7
mail.Body = "Some text goes here";
8
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
9
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
10
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here
11
12
SmtpMail.SmtpServer = "mail.mycompany.com"; //your real server goes here
13
SmtpMail.Send( mail );
14
}
15

2

3

4

5

6

7

8

9

10

11

12

13

14

15

第三、使用Socket撰写邮件发送程序
当然,如果您觉得SmtpMail不能满足您的需求,CDO又不够直截了当,那就只能自己动手了;其实如果您很熟悉Socket编程,自己写一个发送邮件的程序并不很难,以下就是一个例子。
首先,我们简单介绍一下带验证的SMTP服务器如何使用AUTH原语进行身份验证,其详细的定义可以参考RFC2554。
具体如下:
1)首先,需要使用EHLO而不是原先的HELO。
2)EHLO成功以后,客户端需要发送AUTH原语,与服务器就认证时用户名和密码的传递方式进行协商。
3)如果协商成功,服务器会返回以3开头的结果码,这是就可以把用户名和密码传给服务器。
4)最后,如果验证成功,就可以开始发信了。
下面是一个实际的例子,客户端在WinXP的Command窗口中通过"telnet smtp.263.NET 25"命令连接到263的smtp服务器发信:
220 Welcome to coremail System(With Anti-Spam) 2.1
EHLO 263.NET
250-192.168.30.29
250-PIPELINING
250-SIZE 10240000
250-ETRN
250-AUTH LOGIN
250 8BITMIME
AUTH LOGIN
334 VXNlcm5hbWU6
bXlhY2NvdW50
334 UGFzc3dvcmQ6
bXlwYXNzd29yZA==
235 Authentication successful
MAIL FROM:myaccount@263.NET
250 Ok
RCPT TO:myaccount@263.NET
250 Ok
Data
354 End data with
This is a testing email.
haha.
.
250 Ok: queued as AC5291D6406C4
QUIT
221 Bye
上面的内容就是发信的全过程。其中与身份验证有关的主要是第九到第十四行:
AUTH LOGIN "客户端输入
334 VXNlcm5hbWU6 "服务器提示“Username:="
bXlhY2NvdW50 "客户端输入“myaccount="的Base64编码
334 UGFzc3dvcmQ6 "服务器提示“Password:="
bXlwYXNzd29yZA== "客户端输入“mypassword="的Base64编码
235 Authentication successful "服务器端通过验证
从上面的分析可以看出,在这个身份验证过程中,服务器和客户端都直接通过Socket传递经过标准Base64编码的纯文本。这个过程可以非常方便的用C#实现,或者直接添加到原有的源代码中。
另外,有些ESMTP服务器不支持AUTH LOGIN方式的认证,只支持AUTH CRAM-MD5方式验证。但是这两者之间的区别只是文本的编码方式不同。
实现此功能的源代码可以在SourceForge.NET http://sourceforge.NET/projects/opensmtp-net/ 上找到下载。下面给出了一个简单的伪码:
1
public void SendMail(MailMessage msg)
2
<span id=Codehighlighter1_39_2214_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; B%2

2
