在系统目录(如c:\winnt或c:\windows)的system子目录中可以找到一个名称为cdosysdll的文件我们可以通过ASPNET调用此COM组件来实现Email的发送cdosys构建在SMTP协议和NNTP协议之上并且作为Windows Server的组件被安装当然我们也可以使用Exchange中cdoexdll来实现发送邮件的机制由于cdosysdll内嵌到了操作系统中所以不用再去注册相应的其他邮件发送程序比如jmail等
新建一个项目文件
添加引用系统目录下的cdosysdll文件在引用中会发现添加了两个要用到的接口CDOADODB
添加新项文件SendMailaspx在其页面上放置三个Label三个Textbox作用分别为收件人地址主题内容放置一个Button按钮
切换到代码页创建一下内容
public void CDOsendmail()
{
try
{
CDOMessage Msg = new CDOMessage();
MsgFrom = rattlesnake@net;
MsgTo = thisTextBoxTextTrim();
MsgSubject = thisTextBoxTextTrim();
MsgHTMLBody = <html><body>+thisTextBoxText+</body></html>;
CDOIConfiguration Config = MsgConfiguration;
ADODBFields oFields = ConfigFields;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendusing]Value = ;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendusername]Value=rattlesnake;
oFields[http://schemasmicrosoftcom/cdo/configuration/sendpassword]Value=pass;
oFields[http://schemasmicrosoftcom/cdo/configuration/smtpauthenticate]Value=;
oFields[http://schemasmicrosoftcom/cdo/configuration/languagecode]Value=x;
oFields[http://schemasmicrosoftcom/cdo/configuration/smtpserver]Value=smtpnet;
oFieldsUpdate();
MsgBodyPartCharset = gb;
MsgHTMLBodyPartCharset = gb;
MsgSend();
Msg = null;
}
catch(Exception err)
{
throw err;
}
}
为Button添加Click事件
private void Button_Click(object sender SystemEventArgs e)
{
thisCDOsendmail();
}
运行程序即可