简体   繁体   中英

SMTP Server Authentication Error

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?

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 in your google account.

After sign into google account, go to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM