深港DJ音乐社区 » 网站建站教程 » ASP教程 » ASP.NET结合COM组件发送Email
深港DJ音乐论坛诚招版主 您可以由以下地址进入我们的摇头世界
深港DJ俱乐部 深港DJ摇头网
深港音乐社区QQ群:24863889
本页主题: ASP.NET结合COM组件发送Email 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

逍遥浪子
(紫晶之约) 原创先锋奖 (青涩光华) 特殊贡献奖
该用户目前不在线
级别: 管理员
精华: 5
发帖: 464
威望: 737 点
深港币: 398396
贡献值: 0 点
好评度: 404 点
朋友圈: 深港DJ炫舞研究协会
在线时间:345(小时)
注册时间:2007-02-19
最后登录:2009-01-07

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内嵌到了操作系统中,所以不用再去注册相应的其他邮件发送程序比如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 = ""+this.TextBox3.Text+"";
  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();
}

  运行程序即可。

 
顶端 Posted: 2007-03-01 03:37 | [楼 主]
深港DJ音乐社区 » ASP教程

Total 0.056718(s) query 5, phpwindTime now is:01-10 11:38, Gzip enabled 粤ICP备06068709号
Powered by 深港DJ论坛 ik123.comCode © 2003-07 Corporation