SMTP错误5.5.1

时间:2015-07-17 05:54:37

标签: c# email smtp

以下是我使用smtp服务器发送确认邮件的代码i C#!!但总是得到错误5.5.1客户端身份验证!请帮助!!

MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();

try
{
    m.From = new MailAddress("swamycns95@gmail.com");
    m.To.Add(email);
    m.Subject = "This is a Test Mail";
    m.IsBodyHtml = true;
    m.Body = "test gmail";
    sc.Host = "smtp.gmail.com";
    sc.Port = 587;
    sc.EnableSsl = true;
    System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("swamycns95@gmail.com", "cnstvm36");
    sc.Credentials = basicCredential;
    sc.UseDefaultCredentials = true;

    sc.Send(m);
    Response.Write("Email Send successfully");
}
    catch (Exception ex)
    {
        ShowMessage(ex.Message);
    }

1 个答案:

答案 0 :(得分:0)

通过将sc.UseDefaultCredentials设置为true,您告诉它忽略您提供的凭据,而是从app.config加载凭据。

解决方案是将UseDefaultCredentials设置为false

注意:在设置UseDefaultCredentials属性之前,您可能需要将false设置为Credentials 。我不记得你必须设置这些属性的顺序,但我认为这很重要。