ASP.NET結(jié)合COM組件發(fā)送Email
2024-07-10 12:59:01
供稿:網(wǎng)友
在系統(tǒng)目錄(如c:/winnt或c:/windows)的system32子目錄中可以找到一個(gè)名稱為cdosys.dll的文件,我們可以通過asp.net調(diào)用此com組件來實(shí)現(xiàn)email的發(fā)送。cdosys構(gòu)建在smtp協(xié)議和nntp協(xié)議之上,并且作為windows2000 server的組件被安裝,當(dāng)然我們也可以使用exchange2000中cdoex.dll來實(shí)現(xiàn)發(fā)送郵件的機(jī)制,由于cdosys.dll內(nèi)嵌到了操作系統(tǒng)中,所以不用再去注冊(cè)相應(yīng)的其他郵件發(fā)送程序比如jmail等。
1、新建一個(gè)項(xiàng)目文件
2、添加引用系統(tǒng)目錄下的cdosys.dll文件,在引用中會(huì)發(fā)現(xiàn)添加了兩個(gè)要用到的接口:cdo,adodb
3、添加新項(xiàng)文件sendmail.aspx,在其頁面上放置三個(gè)label,三個(gè)textbox,作用分別為收件人地址、主題、內(nèi)容,放置一個(gè)button按鈕。
4、切換到代碼頁,創(chuàng)建一下內(nèi)容
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();
}
運(yùn)行程序即可。
國內(nèi)最大的酷站演示中心!