您现在的位置: 天极网 > 开发频道 > web开发 > ASP.NET结合COM组件发送Email
全文

ASP.NET结合COM组件发送Email

2003-07-07 10:50作者:hbzxf出处:论坛责任编辑:方舟


  在系统目录(如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();
}

  运行程序即可。

软件资讯·软件下载尽在天极软件

相关搜索:
关注此文读者还看过
热门关注
特别推荐
网友关注
软件下载
娱乐下载
驱动下载
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众