Search code examples
c#asp.netemailsmtpgmail

Trying to use SMTP with gmail


I am using smtp with gmail (already setup in my gmail account to allow it). But everytime that a different computer try to do it, gmail says that someone tryed to use my account and ask if it was me (so my confirmation e-mail isn't sended).

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

}

}


Solution

  • GMAIL defaults to not allowing smtp access

    Once you login to your Gmail account, type this into the URL bar:

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

    At the bottom you'll see a setting "Allow less secure apps" Toggle that to "ON"