在系統目錄(如c:\winnt或c:\windows)的system子目錄中可以找到一個名稱為cdosysdll的文件我們可以通過ASPNET調用此COM組件來實現Email的發送cdosys構建在SMTP協議和NNTP協議之上並且作為Windows Server的組件被安裝當然我們也可以使用Exchange中cdoexdll來實現發送郵件的機制由於cdosysdll內嵌到了操作系統中所以不用再去注冊相應的其他郵件發送程序比如jmail等
新建一個項目文件
添加引用系統目錄下的cdosysdll文件在引用中會發現添加了兩個要用到的接口CDOADODB
添加新項文件SendMailaspx在其頁面上放置三個Label三個Textbox作用分別為收件人地址主題內容放置一個Button按鈕
切換到代碼頁創建一下內容
public void CDOsendmail()
{
try
{
CDOMessage Msg = new CDOMessage();
MsgFrom = rattlesnake@net;
MsgTo = thisTextBoxTextTrim();
MsgSubject = thisTextBoxTextTrim();
MsgHTMLBody = <html><body>+thisTextBoxText+</body></html>;
CDOIConfiguration Config = MsgConfiguration;
ADODBFields oFields = ConfigFields;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendusing]Value = ;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendusername]Value=rattlesnake;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendpassword]Value=pass;
oFields[http://schemasmicrosoftcom/cdo/configuration/smtpauthenticate]Value=;
oFields[http://schemasmicrosoftcom/cdo/configuration/languagecode]Value=x;
oFields[http://schemasmicrosoftcom/cdo/configuration/smtpserver]Value=smtpnet;
oFieldsUpdate();
MsgBodyPartCharset = gb;
MsgHTMLBodyPartCharset = gb;
MsgSend();
Msg = null;
}
catch(Exception err)
{
throw err;
}
}
為Button添加Click事件
private void Button_Click(object sender SystemEventArgs e)
{
thisCDOsendmail();
}
運行程序即可
From:http://tw.wingwit.com/Article/program/net/201311/15803.html