ASP.NET結合COM組件發送Email

來源:互聯網
上載者:User
在系統目錄(如c:\winnt或c:\windows)的system32子目錄中可以找到一個名稱為cdosys.dll的檔案,我們可以通過ASP.NET調用此COM組件來實現Email的發送。cdosys構建在SMTP協議和NNTP協議之上,並且作為Windows2000 Server的組件被安裝,當然我們也可以使用Exchange2000中cdoex.dll來實現發送郵件的機制,由於cdosys.dll內嵌到了作業系統中,所以不用再去註冊相應的其他郵件發送程式比如jmail等。

  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();
}

  運行程式即可。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.