ASP.NET结合COM组件发送Email

本文介绍如何使用ASP.NET和CDOSYS.dll组件发送电子邮件。通过配置SMTP服务器等参数,实现从网页界面输入邮件内容并发送的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  运行程序即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值