为什么在asp.net中收到SMTP错误

时间:2016-02-18 05:11:16

标签: c# asp.net smtp smtpclient

我有登录page.login工作正常,但在尝试创建新用户时,我正在

  

错误:SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应是:5.5.1身份验证   必需的。

这是我用来发送邮件的代码

;

任何人都可能出错

2 个答案:

答案 0 :(得分:1)

试试这个:

public void SendConfirmationAfterRegister(String EmailID, String UserName)
{
    MailMessage mailMsg = new MailMessage();

    String BodyMsg = UserName + ", \r\n\nWe have received your request to become a user of our site.  Upon review, we will send you verification for site access.\r\n\n" +
        "Thank you, \r\n\nMuda Admin";
    try
    {
        using (var client = new SmtpClient("smtp.gmail.com", 587))
        {
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
            client.EnableSsl = true;

            mailMsg.IsBodyHtml = true;
            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), EmailID, "Received Your Request", BodyMsg);

            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "mymailId", "User Needs Access", EmailID + " looking for access the xyz Network Site.");
        }
    }
    catch (Exception ex) { throw new Exception(ex.Message); }
}

您需要将UseDefaultCredentials设置为false

注意:在设置UseDefaultCredentials属性之前,您必须将false设置为Credentials

答案 1 :(得分:0)

试试这个......

using System.Net.Mail;

 String BodyMsg = UserName + ", \r\n\nWe have recieved your request to become a user of our site.  Upon review, we will send you verification for site access.\r\n\n" +
        "Thank you, \r\n\nMuda Admin";
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress(ConfigurationManager.AppSettings["AdminEmail"].ToString());
            mail.To.Add("To@domain.com");
            mail.Subject = "subject";
            mail.Body = BodyMsg ;

            SmtpServer.UseDefaultCredentials=true
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);