解讀郵件發送CDO.Message錯誤
2024-07-21 02:25:31
供稿:網友
喜歡上c#,再也沒有理由離開它去學另一種語言,asp中可以方便的調用cdo并附上賬
號和密碼來發送郵件,但system.web.mail命名空間里卻并未讓我等到輸入用戶名和密
碼的屬性,沒有驗證就會出現:cdo.message調用失敗。在觀看了別的同仁的文章,試了一個果然見效,在此與大家分享一下。
由于在.net平臺上并不在于程序寫多少,更不在于用什么語言去表達,重要的似乎是思
想,所以我喜歡c#也只用c#寫這幾句代碼吧,vb.net與j#的朋友可以稍微改一下即
可用了......
private static int gotosendmail(string body,string to)
{
try
{
system.web.mail.mailmessage mm=new system.web.mail.mailmessage();
mm.bodyformat=system.web.mail.mailformat.html;
mm.from="[email protected]";
mm.to=to;
mm.bodyencoding=system.text.encoding.getencoding(936);
mm.subject="您好!我是夢貓.net工作室希望與您攜手一起成長。";
mm.body=body;
mm.fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] =
2;
mm.fields
["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"] =
"[email protected]";//發送地址;如果mm.from寫了這兒可以不寫這句
mm.fields
["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"] =
"[email protected]";
mm.fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]
= "xxx";//驗證賬號:發送者郵箱賬號
mm.fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]
= "xxxpass"; //驗證密碼:發送者郵箱密碼
mm.fields
["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; //
驗證級別0,1,2
mm.fields["http://schemas.microsoft.com/cdo/configuration/languagecode"]
= 0x0804;//語言代碼
mm.fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] =
"smtp.xxx.com"; //smtp server
system.web.mail.smtpmail.smtpserver="smtp.xxx.com";//上句和這句重著,這
句可以替代上句
system.web.mail.smtpmail.send(mm);
return 0;
}
catch(system.exception e)
{
response.write(e.message+e.stacktrace+e.source);
return -1;
}
}
本程序在xp和2000server iis6上均通過