代码改变世界

asp.net在发送邮件时出现"服务器响应为: You are not authorized to send mail, authentication is required "解决方案

2010-02-27 00:22  ☆冷枫☆  阅读(2159)  评论(0编辑  收藏  举报

      今天在处理一个asp.net发送邮件的程序时,总是出现"服务器响应为: You are not authorized to send mail, authentication is required "的问题,在网上收集了一些资料最终得以解决。具体解决方案如下:

       首先调用的相应的发送代码-->

         

    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="form">发送的邮件地址</param>
    /// <param name="to">需要发送到的邮件地址</param>
    /// <param name="subject">邮件标题内容</param>
    /// <param name="body">邮件主体内容</param>
    /// <returns>成功则返回true</returns>
    public bool SendEmail(string from, string to, string subject, string body)
    {
        try
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new System.Net.Mail.MailAddress(from, "zzxbest@126.com");
            msg.To.Add(to);
            msg.Subject = subject;
            msg.Body = body;
            msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            msg.Priority = System.Net.Mail.MailPriority.High;
            System.Net.Mail.SmtpClient cliect = new System.Net.Mail.SmtpClient("smtp.126.com");
            cliect.Credentials = new System.Net.NetworkCredential("zzxbest@126.com", "******");//登陆的邮箱名和密码          

            cliect.Send(msg);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        return false;

    }

 

   如果标注的红色部分没有写正确,或者写的邮箱没有和相应的邮箱服务器对应"smtp.xxx.com"对应,也会出现相应的问题。

    特此记录,备忘!