在vs 2003中,我欲实现发送邮件功能,现在出现下面的问题:我用163邮箱可以实现正常发送邮件,但是我用126邮箱就会报错。
下面是 163邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名"); 
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码"); //下面设置一下发送邮件的服务器
try 
{
SmtpMail.SmtpServer = "smtp.163.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}下面是 126邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名1"); 
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码1"); //下面设置一下发送邮件的服务器
try 
{
SmtpMail.SmtpServer = "smtp.126.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}用126邮箱时,出现下面的错误“发送失败未能访问“CDO.Message”对象。”,请问这是什么原因啊?

解决方案 »

  1.   

    申明:
    using System.Net;
    using System.Net.Mail;
    方法:
            public bool ShowPost()//gmail的发送方法
            {
                string from = "[email protected]";
                string pwd = "7420861467wang";
                string to = TxtTo.Text.Trim();
                MailMessage mail = new MailMessage(from,to);
                mail.Subject = TxtHead.Text;
                mail.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
                mail.Body = TxtBody.Text;
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
                mail.IsBodyHtml = true;
                string server = "";
                int port = 25;
                if (from.IndexOf("@gpres")!=-1 || from.IndexOf("@gmail")!=-1)
                {
                    port = 587;
                    server = "smtp.gmail.com";
                }
                else 
                {
                    port = 25;
                    string temp;
                    temp=from.Substring(from.LastIndexOf('@')+1);
                    server = "smtp." + temp;
                }
                SmtpClient sc = new SmtpClient(server, port);
                if (port!=25)
                {
                    sc.EnableSsl = true;
                }
                sc.Credentials = new NetworkCredential(from, pwd);
                try 
                {
                    sc.Send(mail);
                    mail.Dispose();
                    return true;
                }
                catch(Exception ex)
                {
                    //System.Web.HttpContext.Current.Response.Write(e.Message);
                    mail.Dispose();
                    return false;
                }
                
            }
      

  2.   

    SmtpClient 更加不行,很多邮件箱的都发不了。
    首先你要看你拿到的126的这个smtp服务器支不支持用程序发送邮件。很多邮件箱的服务器是禁止的,担心你利用它的邮箱群发垃圾邮箱。如果你能用163的发,代码应该没有问题。查下126的邮箱可不可以发吧。
      

  3.   

    首先你试着用outlook发,看能不能发,如果连outlook都不能发,那肯定不能发。有时候outlook能发,程序也有可能不能发。
    你只能再注册一个其他邮件商的邮箱地址测试了,如果能发,那说明126的服务器不能发。像21cn,sohu.com,sina.com,这些邮箱好像都可以发,只是一天不能发太多,发太多它就认为你发的是垃圾邮件了。