c#.NET中發送郵件

在.NET中發送郵件
摘要
本文簡單介紹了SMTP協議(RFC2554)發送郵件的過程,並討論了在.NET中使用SMTP發送郵件由簡到繁的三種不同方案、各自可能遇到的問題及其解決辦法。
目錄
?.NET的SMTP類
?使用CDO組件發送郵件
?使用Socket撰寫郵件發送程序
總結
簡介
郵件發送功能常常是許多.NET應用,尤其是帶網絡功能的應用中不可缺少的模塊之一,本文就此介紹了使用.NET的SMTP類庫和另兩種分別通過CDO(Collaboration DataObjects)及Socket來實現發送郵件功能的方法。

.NET的SMTP類
首先,我們來介紹一下.NET類庫種自帶的SMTP類。在.NET中的System.Web.Mail名字空間下,有一個專門使用SMTP協議來發送郵件的類:SmtpMail,它已能滿足最普通的發送郵件的需求。這個類只有一個自己的公共函數--Send()和一個公共屬性—SmtpServer,如下圖:
您必須通過SmtpServer屬性來指定發送郵件的服務器的名稱(或IP地址),然後再調用
Send()函數來發送郵件。
代碼示例如下:
(inC#)
usingSystem.Web.Mail;
publicvoidsendMail()
{
try
{
System.Web.Mail.MailMessagemyMail=newMailMessage();
myMail.From="[email protected]";
myMail.To="[email protected]";
myMail.Subject="MailTest";
myMail.Priority=MailPriority.Low;
myMail.BodyFormat=MailFormat.Text;
myMail.Body="Test";
SmtpMail.SmtpServer="smarthost";//yoursmtpserverhere

SmtpMail.Send(myMail);
}
catch(Exceptione)
{
throwe;
}
}

您可以在Send函數的參數MailMessage對象中設置郵件的相關屬性,如優先級、附件等等。除了以MailMessage對象爲參數(如上述代碼),Send函數還可以簡單的直接以郵件的4個主要信息(from,to,subject,messageText)作爲字符串參數來調用。
使用CDO組件發送郵件
CDO是CollaborationDataObjects的簡稱,它是一組高層的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服務器發送郵件的過程:
(inC#)
publicvoidCDOsendMail()
{
try
{
CDO.MessageoMsg=newCDO.Message();

oMsg.From="[email protected]";
oMsg.To="[email protected]";
oMsg.Subject="MailTest";

oMsg.HTMLBody="<html><body>Test</body></html>";

CDO.IConfigurationiConfg=oMsg.Configuration;
ADODB.FieldsoFields=iConfg.Fields;

oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="[email protected]"; //sendermailoFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="[email protected]"; //emailaccountoFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username"; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password"; oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
//value=0代表Anonymous驗證方式(不需要驗證)
//value=1代表Basic驗證方式(使用basic(clear-text)authentication.
//Theconfigurationsendusername/sendpasswordorpostusername/postpassword fieldsareusedtospecifycredentials.)
//Value=2代表NTLM驗證方式(SecurePasswordAuthenticationinMicrosoftOutlookExpress)
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com";

oFields.Update();
oMsg.BodyPart.Charset="gb2312";
oMsg.HTMLBodyPart.Charset="gb2312";

oMsg.Send();
oMsg=null;
}
catch(Exceptione)
{
throwe;
}
}

注意:由於Exchange2000的CDO組件cdoex.dll會更新原有的Windows2000的CDO組件cdosys.dll,所以如果您希望繼續使用cdosys.dll,您必須先通過regsrv32.exe卸載掉cdoex.dll。

使用Socket撰寫郵件發送程序
當然,如果您覺得SmtpMail不能滿足您的需求,CDO又不夠直截了當,那就只能自己動手了;其實如果您很熟悉Socket編程,自己寫一個發送郵件的程序並不很難,以下就是一個例子。

首先,我們簡單介紹一下帶驗證的SMTP服務器如何使用AUTH原語進行身份驗證,其詳細的定義可以參考RFC2554。

具體如下:

1)首先,需要使用EHLO而不是原先的HELO。

2)EHLO成功以後,客戶端需要發送AUTH原語,與服務器就認證時用戶名和密碼的傳遞方式進行協商。

3)如果協商成功,服務器會返回以3開頭的結果碼,這是就可以把用戶名和密碼傳給服務器。

4)最後,如果驗證成功,就可以開始發信了。

下面是一個實際的例子,客戶端在WinXP的Command窗口中通過“telnetsmtp.263.NET25="命令連接到263的smtp服務器發信:

220WelcometocoremailSystem(WithAnti-Spam)2.1
EHLO263.NET
250-192.168.30.29
250-PIPELINING
250-SIZE10240000
250-ETRN
250-AUTHLOGIN
2508BITMIME
AUTHLOGIN
334VXNlcm5hbWU6
bXlhY2NvdW50
334UGFzc3dvcmQ6
bXlwYXNzd29yZA==
235Authenticationsuccessful
MAILFROM:[email protected]
250Ok
RCPTTO:[email protected]
250Ok
Data
354Enddatawith<CR><LF>.<CR><LF>
Thisisatestingemail.
haha.
.
250Ok:queuedasAC5291D6406C4
QUIT
221Bye

上面的內容就是發信的全過程。其中與身份驗證有關的主要是第九到第十四行:

AUTHLOGIN''''客戶端輸入

334VXNlcm5hbWU6''''服務器提示“Username:="

bXlhY2NvdW50''''客戶端輸入“myaccount="的Base64編碼

334UGFzc3dvcmQ6''''服務器提示“Password:="

bXlwYXNzd29yZA==''''客戶端輸入“mypassword="的Base64編碼

235Authenticationsuccessful''''服務器端通過驗證

從上面的分析可以看出,在這個身份驗證過程中,服務器和客戶端都直接通過Socket傳遞經過標準Base64編碼的純文本。這個過程可以非常方便的用C#實現,或者直接添加到原有的源代碼中。

另外,有些ESMTP服務器不支持AUTHLOGIN方式的認證,只支持AUTHCRAM-MD5方式驗證。但是這兩者之間的區別只是文本的編碼方式不同。

實現此功能的源代碼可以在SourceForge.NEThttp://sourceforge.NET/projects/opensmtp-net/ 上找到下載。下面給出了一個簡單的僞碼:

publicvoidSendMail(MailMessagemsg)
{
NetworkStreamnwstream=GetConnection();

WriteToStream(refnwstream,"EHLO"+smtpHost+"/r/n");
stringwelcomeMsg=ReadFromStream(refnwstream);

//implementHELOcommandifEHLOisunrecognized.
if(IsUnknownCommand(welcomeMsg))
{
WriteToStream(refnwstream,"HELO"+smtpHost+"/r/n");
}
CheckForError(welcomeMsg,ReplyConstants.OK);

//Authenticationisusediftheu/paresupplied
AuthLogin(refnwstream);

WriteToStream(refnwstream,"MAILFROM:<"+msg.From.Address +">/r/n");
CheckForError(ReadFromStream(refnwstream),ReplyConstants.OK);

SendRecipientList(refnwstream,msg.To);
SendRecipientList(refnwstream,msg.CC);
SendRecipientList(refnwstream,msg.BCC);

WriteToStream(refnwstream,"DATA/r/n");
CheckForError(ReadFromStream(refnwstream),ReplyConstants.START_INPUT);

if(msg.ReplyTo.Name!=null&&msg.ReplyTo.Name.Length!= 0)
{WriteToStream(refnwstream,"Reply-To:/""+msg.ReplyTo.Name +"/"<"+
msg.ReplyTo.Address+">/r/n");}
else
{WriteToStream(refnwstream,"Reply-To:<"+msg.ReplyTo.Address +">/r/n");}

if(msg.From.Name!=null&&msg.From.Name.Length!=0)
{WriteToStream(refnwstream,"From:/""+msg.From.Name+"/"<"+
msg.From.Address+">/r/n");}
else
{WriteToStream(refnwstream,"From:<"+msg.From.Address+">/r/n"); }

WriteToStream(refnwstream,"To:"+CreateAddressList(msg.To)+"/r/n");

if(msg.CC.Count!=0)
{WriteToStream(refnwstream,"CC:"+CreateAddressList(msg.CC) +"/r/n");}

WriteToStream(refnwstream,"Subject:"+msg.Subject+"/r/n");

if(msg.Priority!=null)
{WriteToStream(refnwstream,"X-Priority:"+msg.Priority+"/r/n"); }

if(msg.Headers.Count>0)
{
SendHeaders(refnwstream,msg);
}

if(msg.Attachments.Count>0||msg.HtmlBody!=null)
{
SendMessageBody(refnwstream,msg);
}
else
{
WriteToStream(refnwstream,msg.Body+"/r/n");
}

WriteToStream(refnwstream,"/r/n./r/n");
CheckForError(ReadFromStream(refnwstream),ReplyConstants.OK);


WriteToStream(refnwstream,"QUIT/r/n");
CheckForError(ReadFromStream(refnwstream),ReplyConstants.QUIT);

CloseConnection();
}

privateboolAuthLogin(refNetworkStreamnwstream)
{
if(username!=null&&username.Length>0&&password !=null&&password.Length>0)
{
WriteToStream(refnwstream,"AUTHLOGIN/r/n");
if(AuthImplemented(ReadFromStream(refnwstream)))
{
WriteToStream(refnwstream,Convert.ToBase64String(
Encoding.ASCII.GetBytes(this.username.ToCharArray()))+"/r/n");

CheckForError(ReadFromStream(refnwstream),ReplyConstants.SERVER_CHALLENGE);

WriteToStream(refnwstream,Convert.ToBase64String(Encoding.ASCII.GetBytes(
this.password.ToCharArray()))+"/r/n");
CheckForError(ReadFromStream(refnwstream),ReplyConstants.AUTH_SUCCESSFUL);
returntrue;
}
}
returnfalse;
}

總結

本文介紹了.NET中三種不同的使用SMTP協議發送郵件的方法,其中第一種(使用SmtpMail類)方案能滿足大部分基本的發送郵件的功能需求,而第二種(使用CDO組件)和第三種(使用Socket自己撰寫SMTP類)方案提供更自由和完整的定製方法,比如他們都能實現第一種方案不能做到的通過帶認證的SMTP服務器發送郵件的功能。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章