2016-02-18 2 views
0

Я войти в систему page.login работает нормально, но при попытке создать нового пользователя, я получаюпочему получение SMTP ошибки в asp.net

Error:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Вот код, который я использую для отправки почты

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

     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"; 
     try 
     { 
      var client = new SmtpClient("smtp.gmail.com", 587) 
      { 
       Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString()), 
       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); } 
    } 

может любой, что это неправильно

+0

Попробуйте сменить номер порта на 25 – Sankar

ответ

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 собственности.

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("[email protected]"); 
      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); 
+0

Нет снова получения такой же ошибки –

+0

Я не могу исправить ошибку. После установки UseDefaultCredentials = false также я получаю сообщение об ошибке: Ошибка отправки почты –

+0

Я не говорил вам устанавливать 'UseDefaultCredentials = false'. Я думаю, что этот комментарий должен быть опубликован для решения @jstedfast. – Sankar