0

I'm working on an app where I would like to send a crash report to an email if a global threading exception is invoked. However, every time I try to send this email, I get the error

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at. <-- the error message ends here which seems weird.

My exception event handler looks like this:

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MailAddress fromAddress = new MailAddress("user@domain.com", "Mary");
        MailAddress toAddress = new MailAddress("user@domain.com", "Sam");
        const string fromPassword = "password";
        const string subject = "Exception Report";
        Exception exception = e.ExceptionObject as Exception;
        string body = exception.Message + "\n" + exception.Data + "\n" + exception.StackTrace + "\n" + exception.Source;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (MailMessage message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            try
            { 
                smtp.Send(message);
            }
           catch(Exception err)
            {
                Console.WriteLine("Could not send e-mail. Exception caught: " + err);
            }
        }
    }

Does anyone know why I would be getting this server authentication error?

andyopayne
  • 1,348
  • 3
  • 24
  • 49

1 Answers1

1

Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.

Some examples of apps that do not support the latest security standards include:

  • The Mail app on your iPhone or iPad with iOS 6 or below
  • The Mail app on your Windows phone preceding the 8.1 release
  • Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird

Therefore, you have to enable Less Secure Sign-In in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps

meda
  • 45,103
  • 14
  • 92
  • 122
  • Ok. But, now I have a follow up question. Right now, I'm using a dummy email account to send these emails from. Ultimately, this is going to be rolled into a stand-alone desktop application which will hopefully send an email to another dedicated email address setup to accept these crash reports. Is this the correct way to go about this (ie. using dummy accounts, etc...) or is there a more standard way of doing this? – andyopayne Jan 05 '16 at 20:06
  • The way I do it, I move the email address and password to an app config file, this allows you to make changes without touching your code – meda Jan 05 '16 at 20:08