在.net framework 1.x 我們需要使用 system.web.mail 命名空間下的類 來進行發送郵件,但是功能比較弱,比如你的郵件服務器需要驗證才能發送郵件,在.net 1.1 中,需要用下面的代碼來做額外配置。
mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
, "1");mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername"
, "my_username_here");mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword"
, "super_secret");
.net 1.x 下發送郵件的方式請參考:
http://blog.joycode.com/joy/archive/2004/01/14/11405.aspx
.net framework 2.0 下,在 system.net.mail 命名空間中提供了對郵件操作的支持,他的功能更強大。比如你的郵件服務器需要驗證才能發送郵件,代碼就只需簡單成如下:
public static void sendsmtpemail(string strsmtpserver, string strfrom,
string strfrompass, string strto, string strsubject, string strbody)...{
system.net.mail.smtpclient client = new smtpclient(strsmtpserver);
client.usedefaultcredentials = false;
client.credentials =
new system.net.networkcredential(strfrom, strfrompass);client.deliverymethod = smtpdeliverymethod.network;

system.net.mail.mailmessage message =
new mailmessage(strfrom, strto, strsubject, strbody);message.bodyencoding = system.text.encoding.utf8;
message.isbodyhtml = true;
client.send(message);
}
我們可以通過修改 usedefaultcredentials credentials deliverymethod 等屬性,方便的支持各種情況下發送郵件的方式。
新聞熱點
疑難解答
圖片精選