2016-07-05 5 views
2

Я использую smtp с gmail (уже настроен в моей учетной записи gmail, чтобы разрешить его). Но каждый раз, когда другой компьютер пытается это сделать, gmail говорит, что кто-то пытался использовать мою учетную запись и спрашивать, был ли это я (так что мое подтверждение по электронной почте не отдается).Пытается использовать SMTP с gmail

using Microsoft.AspNet.Identity; 
using System; 
using System.Configuration; 
using System.Net; 
using System.Net.Mail; 
using System.Threading.Tasks; 

namespace Prac.Services 
{ 
public class EMail : IIdentityMessageService 
{ 

    #region Private Fields 

    private static string FromAddress; 
    private static string strSmtpClient; 
    private static string UserID; 
    private static string Password; 
    private static string SMTPPort; 
    private static bool bEnableSSL; 

    #endregion 

    #region Interface Implementation 

    public async Task SendAsync(IdentityMessage message) 
    { 
     await configSendGridasync(message); 
    } 

    #endregion 

    #region Send Email Method 
    public async Task configSendGridasync(IdentityMessage message) 
    { 
     GetMailData(); 
     dynamic MailMessage = new MailMessage(); 
     MailMessage.From = new MailAddress(FromAddress); 
     MailMessage.To.Add(message.Destination); 
     MailMessage.Subject = message.Subject; 
     MailMessage.IsBodyHtml = true; 
     MailMessage.Body = message.Body; 

     SmtpClient SmtpClient = new SmtpClient(); 
     SmtpClient.Host = strSmtpClient; 
     SmtpClient.EnableSsl = bEnableSSL; 
     SmtpClient.Port = Int32.Parse(SMTPPort); 
     SmtpClient.Credentials = new NetworkCredential(UserID, Password); 

     try 
     { 
      try 
      { 
       SmtpClient.Send(MailMessage); 

      } 
      catch (Exception ex) 
      { 

      } 
     } 
     catch (SmtpFailedRecipientsException ex) 
     { 
      for (int i = 0; i <= ex.InnerExceptions.Length; i++) 
      { 
       SmtpStatusCode status = ex.StatusCode; 
       if ((status == SmtpStatusCode.MailboxBusy) | (status == SmtpStatusCode.MailboxUnavailable)) 
       { 
        System.Threading.Thread.Sleep(5000); 
        SmtpClient.Send(MailMessage); 
       } 
      } 
     } 

    } 

    #endregion 

    #region Get Email provider data From Web.config file 
    private static void GetMailData() 
    { 
     FromAddress = ConfigurationManager.AppSettings.Get("FromAddress"); 
     strSmtpClient = ConfigurationManager.AppSettings.Get("SmtpClient"); 
     UserID = ConfigurationManager.AppSettings.Get("UserID"); 
     Password = ConfigurationManager.AppSettings.Get("Password"); 
     //ReplyTo = System.Configuration.ConfigurationManager.AppSettings.Get("ReplyTo"); 
     SMTPPort = ConfigurationManager.AppSettings.Get("SMTPPort"); 
     if ((ConfigurationManager.AppSettings.Get("EnableSSL") == null)) 
     { 
     } 
     else 
     { 
      if ((System.Configuration.ConfigurationManager.AppSettings.Get("EnableSSL").ToUpper() == "YES")) 
      { 
       bEnableSSL = true; 
      } 
      else 
      { 
       bEnableSSL = false; 
      } 
     } 
    } 
    #endregion 

} 

}

+0

«От адреса», UserID и пароль все должны быть из той же учетной записи пользователя. Убедитесь, что «From Address» соответствует «UserID». – jdweng

ответ

1

GMAIL по умолчанию не позволяет доступ Smtp

После того, как вы войти в свой аккаунт Gmail, введите это в адресной строке:

https://myaccount.google.com/u/1/security?hl=en#connectedapps

В нижней части вы увидите параметр «Разрешить менее безопасные приложения» Переключение , что «ON»