解决.NET平台邮件发送CDO.Message错误

喜欢上C#,再也没有理由离开它去学另一种语言,asp中可以方便的调用CDO并附上账号和密码来发送邮件,但System.Web.Mail命名空间里却并未让我等到输入用户名和密码的属性,在观看了别的同仁的文章,试了一个果然见效,在此与大家分享一下。
由于在.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"] = "XXX"; //验证密码:发送者邮箱密码
    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上均通过
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值