ASP.NET结合COM组件发送Email
2024-07-10 12:59:01
供稿:网友
 
在系统目录(如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 = "[email protected]";
  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();
} 
  运行程序即可。
国内最大的酷站演示中心!