解读邮件发送CDO.Message错误

来源:岁月联盟 编辑:zhu 时间:2004-12-30
喜欢上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="XXX@XXX.com";
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"] =

"XXX@XXX.com";//发送地址;如果mm.From写了这儿可以不写这句
mm.Fields

["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"] =

"XXX@XXX.com";
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上均通过