国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.net發送Email

2024-07-10 13:04:36
字體:
來源:轉載
供稿:網友

首先,我們來介紹一下.net類庫種自帶的smtp類。
  在.net中的system.web.mail名字空間下,有一個專門使用smtp協議來發送郵件的類:smtpmail,它已能滿足最普通的發送郵件的需求。這個類只有一個自己的公共函數--send()和一個公共屬性—smtpserver
您必須通過smtpserver屬性來指定發送郵件的服務器的名稱(或ip地址),然后再調用
send()函數來發送郵件。
代碼示例如下:
(in c#)
using system.web.mail;
public void sendmail()
{
try
{
system.web.mail.mailmessage mymail=new mailmessage();
mymail.from = "[email protected]";
mymail.to = "[email protected]";
mymail.subject = "mailtest";
mymail.priority = mailpriority.low;
mymail.bodyformat = mailformat.text;
mymail.body = "test";
smtpmail.smtpserver="smarthost"; //your smtp server here


smtpmail.send(mymail);
}
catch(exception e)
{
throw e;
}
}
您可以在send函數的參數mailmessage對象中設置郵件的相關屬性,如優先級、附件等等。除了以mailmessage對象為參數(如上述代碼),send函數還可以簡單的直接以郵件的4個主要信息(from,to,subject,messagetext)作為字符串參數來調用。


第二、使用cdo組件發送郵件
  cdo是collaboration data objects的簡稱,它是一組高層的com對象集合,并經歷了好幾個版本的演化,現在在windows2000和exchange2000中使用的都是cdo2.0的版本(分別為cdosys.dll和cdoex.dll)。cdosys構建在smtp協議和nntp協議之上,并且作為windows2000 server的組件被安裝,您可以在系統目錄(如c:/winnt或c:/windows)的system32子目錄中找到它(cdosys.dll)。
  cdo組件相對于先前介紹的smtpmail對象功能更為豐富,并提供了一些smtpmail類所沒有提供的功能,如通過需要認證的smtp服務器發送郵件等。
下面一段代碼就展示了如何使用cdo組件通過需要認證的smtp服務器發送郵件的過程:
(in c#)
public void cdosendmail()
{
try
{
cdo.message omsg = new cdo.message();

omsg.from = "[email protected]";
omsg.to = "[email protected]";
omsg.subject = "mailtest";

omsg.htmlbody = "test";


cdo.iconfiguration iconfg = omsg.configuration;
adodb.fields ofields = iconfg.fields;

ofields["http://schemas.microsoft.com/cdo/configuration/sendusing"].value=2;
ofields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].value="[email protected]"; //sender mail
ofields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].value="[email protected]"; //email account
ofields["http://schemas.microsoft.com/cdo/configuration/sendusername"].value="username";
ofields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].value="password";
ofields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].value=1;
//value=0 代表anonymous驗證方式(不需要驗證)
//value=1 代表basic驗證方式(使用basic (clear-text) authentication.
//the configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//value=2 代表ntlm驗證方式(secure password authentication in microsoft outlook express)
ofields["http://schemas.microsoft.com/cdo/configuration/languagecode"].value=0x0804;
ofields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].value="smtp.21cn.com";


ofields.update();
omsg.bodypart.charset="gb2312";
omsg.htmlbodypart.charset="gb2312";


omsg.send();
omsg = null;
}
catch (exception e)
{
throw e;
}
}
注意:由于exchange2000的cdo組件cdoex.dll會更新原有的windows2000的cdo組件cdosys.dll,所以如果您希望繼續使用cdosys.dll,您必須先通過regsrv32.exe卸載掉cdoex.dll。


第三、使用socket撰寫郵件發送程序
  當然,如果您覺得smtpmail不能滿足您的需求,cdo又不夠直截了當,那就只能自己動手了;其實如果您很熟悉socket編程,自己寫一個發送郵件的程序并不很難,以下就是一個例子。
首先,我們簡單介紹一下帶驗證的smtp服務器如何使用auth原語進行身份驗證,其詳細的定義可以參考rfc2554。
具體如下:
1)首先,需要使用ehlo而不是原先的helo。
2)ehlo成功以后,客戶端需要發送auth原語,與服務器就認證時用戶名和密碼的傳遞方式進行協商。
3)如果協商成功,服務器會返回以3開頭的結果碼,這是就可以把用戶名和密碼傳給服務器。
4)最后,如果驗證成功,就可以開始發信了。
下面是一個實際的例子,客戶端在winxp的command窗口中通過"telnet smtp.263.net 25"命令連接到263的smtp服務器發信:
220 welcome to coremail system(with anti-spam) 2.1
ehlo 263.net
250-192.168.30.29
250-pipelining
250-size 10240000
250-etrn
250-auth login
250 8bitmime
auth login
334 vxnlcm5hbwu6
bxlhy2nvdw50
334 ugfzc3dvcmq6
bxlwyxnzd29yza==
235 authentication successful
mail from:[email protected]
250 ok
rcpt to:[email protected]
250 ok
data
354 end data with .
this is a testing email.
haha.
.
250 ok: queued as ac5291d6406c4
quit
221 bye


上面的內容就是發信的全過程。其中與身份驗證有關的主要是第九到第十四行:
auth login "客戶端輸入
334 vxnlcm5hbwu6 "服務器提示“username:="
bxlhy2nvdw50 "客戶端輸入“myaccount="的base64編碼
334 ugfzc3dvcmq6 "服務器提示“password:="
bxlwyxnzd29yza== "客戶端輸入“mypassword="的base64編碼
235 authentication successful "服務器端通過驗證
從上面的分析可以看出,在這個身份驗證過程中,服務器和客戶端都直接通過socket傳遞經過標準base64編碼的純文本。這個過程可以非常方便的用c#實現,或者直接添加到原有的源代碼中。
另外,有些esmtp服務器不支持auth login方式的認證,只支持auth cram-md5方式驗證。但是這兩者之間的區別只是文本的編碼方式不同。
實現此功能的源代碼可以在sourceforge.net http://sourceforge.net/projects/opensmtp-net/ 上找到下載。下面給出了一個簡單的偽碼:
public void sendmail(mailmessage msg)
{
networkstream nwstream = getconnection();


writetostream(ref nwstream, "ehlo " + smtphost + "/r/n");
string welcomemsg = readfromstream(ref nwstream);


// implement helo command if ehlo is unrecognized.
if (isunknowncommand(welcomemsg))
{
writetostream(ref nwstream, "helo " + smtphost + "/r/n");
}
checkforerror(welcomemsg, replyconstants.ok);


// authentication is used if the u/p are supplied
authlogin(ref nwstream);


writetostream(ref nwstream, "mail from: <" + msg.from.address + ">/r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.ok);


sendrecipientlist(ref nwstream, msg.to);
sendrecipientlist(ref nwstream, msg.cc);
sendrecipientlist(ref nwstream, msg.bcc);


writetostream(ref nwstream, "data/r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.start_input);


if (msg.replyto.name != null && msg.replyto.name.length != 0)
{ writetostream(ref nwstream, "reply-to: /"" + msg.replyto.name + "/" <" +
msg.replyto.address + ">/r/n"); }
else
{ writetostream(ref nwstream, "reply-to: <" + msg.replyto.address + ">/r/n"); }

if (msg.from.name != null && msg.from.name.length != 0)
{ writetostream(ref nwstream, "from: /"" + msg.from.name + "/" <" +
msg.from.address + ">/r/n"); }
else
{ writetostream(ref nwstream, "from: <" + msg.from.address + ">/r/n"); }

writetostream(ref nwstream, "to: " + createaddresslist(msg.to) + "/r/n");

if (msg.cc.count != 0)
{ writetostream(ref nwstream, "cc: " + createaddresslist(msg.cc) + "/r/n"); }


writetostream(ref nwstream, "subject: " + msg.subject + "/r/n");


if (msg.priority != null)
{ writetostream(ref nwstream, "x-priority: " + msg.priority + "/r/n"); }


if (msg.headers.count > 0)
{
sendheaders(ref nwstream, msg);
}

if (msg.attachments.count > 0 || msg.htmlbody != null)
{
sendmessagebody(ref nwstream, msg);
}
else
{
writetostream(ref nwstream, msg.body + "/r/n");
}

writetostream(ref nwstream, "/r/n./r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.ok);
writetostream(ref nwstream, "quit/r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.quit);
closeconnection();
}


private bool authlogin(ref networkstream nwstream)
{
if (username != null && username.length > 0 && password != null && password.length > 0)
{
writetostream(ref nwstream, "auth login/r/n");
if (authimplemented(readfromstream(ref nwstream)))
{
writetostream(ref nwstream, convert.tobase64string(
encoding.ascii.getbytes(this.username.tochararray())) + "/r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.server_challenge);
writetostream(ref nwstream, convert.tobase64string(encoding.ascii.getbytes(
this.password.tochararray())) + "/r/n");
checkforerror(readfromstream(ref nwstream), replyconstants.auth_successful);
return true;
}
}
return false;
}
--------------------------------------------------------------------------------
總結
本文介紹了.net中三種不同的使用smtp協議發送郵件的方法,其中第一種(使用smtpmail類)方案能滿足大部分基本的發送郵件的功能需求,而第二種(使用cdo組件)和第三種(使用socket自己撰寫smtp類)方案提供更自由和完整的定制方法,比如他們都能實現第一種方案不能做到的通過帶認證的smtp服務器發送郵件的功能。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恩施市| 宁强县| 新巴尔虎左旗| 沅陵县| 崇文区| 烟台市| 龙海市| 睢宁县| 黄龙县| 镶黄旗| 沙洋县| 光山县| 潮安县| 蒲江县| 常州市| 玛纳斯县| 绍兴县| 鹤庆县| 电白县| 九龙坡区| 墨竹工卡县| 满城县| 余干县| 东辽县| 安龙县| 天祝| 石河子市| 屏东县| 金湖县| 永靖县| 太原市| 华宁县| 舟曲县| 三穗县| 西畴县| 咸丰县| 沙雅县| 泸溪县| 平顺县| 微山县| 汕头市|