ASP.NET结合COM组件发送Email
????? 在系統目錄(如c:/winnt或c:/windows)的system32子目錄中可以找到一個名稱為cdosys.dll的文件,我們可以通過ASP.NET調用此COM組件來實現Email的發送。cdosys構建在SMTP協議和NNTP協議之上,并且作為Windows2000 Server的組件被安裝,當然我們也可以使用Exchange2000中cdoex.dll來實現發送郵件的機制。由于cdosys.dll自動內嵌到了操作系統中,所以不用再去注冊相應的其他發送程序,下面我們來做一個發送實例。
??? 1、新建一個項目文件
??? 2、添加引用系統目錄下的cdosys.dll文件,在引用中會發現添加了兩個要用到的接口:CDO,ADODB
??? 3、添加新項文件SendMail.aspx,在其頁面上放置三個Label,三個Textbox,作用分別為收件人地址、主題、內容,放置一個Button按鈕。
??? 4、切換到代碼頁,創建一下內容
??public void CDOsendmail()
??{
???try
???{
????CDO.Message Msg = new CDO.Message();
????Msg.From = "rattlesnake@263.net";
????Msg.To = this.TextBox1.Text.Trim();
????Msg.Subject = this.TextBox2.Text.Trim();
????Msg.HTMLBody = "<html><body>"+this.TextBox3.Text
+"</body></html>";
????CDO.IConfiguration Config = Msg.Configuration;
????ADODB.Fields oFields = Config.Fields;
????oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
????oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="rattlesnake";
????oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="pass";
????oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
????oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
????oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.263.net";
????oFields.Update();
????Msg.BodyPart.Charset = "gb2312";
????Msg.HTMLBodyPart.Charset = "gb2312";
????Msg.Send();
????Msg = null;
???}
???catch(Exception err)
???{
?????? throw err;
???}
??}
??? 5、為Button添加Click事件
??private void Button1_Click(object sender, System.EventArgs e)
??{
????? this.CDOsendmail();
??}
??????? 運行程序,然后檢查郵箱即可。
總結
以上是生活随笔為你收集整理的ASP.NET结合COM组件发送Email的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用VS.NET2003编写存储过程
- 下一篇: 网站信息统计的简单实现过程