Search code examples
c#gmail

Gmail sending error (C#)


I'm trying to send an email via C# but I keep on getting this 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 stack trace can be found here:

System.Net.Mail.SmtpException: 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

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)

at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)

at System.Net.Mail.SmtpClient.Send(MailMessage message)

at Repetrel.View.EmailForm.btnSend_Click(Object sender, EventArgs e) in c:\2015 Hyrel Projects\Repetrel\src\Repetrel\View\EmailForm.cs:line 238

Here is my code:

            NetworkCredential loginInfo = new NetworkCredential(Properties.Settings.Default.MyEmailAddress, Properties.Settings.Default.MyEmailPassword);
            //System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(Properties.Settings.Default.SMTPAddress);
            ////Trace.WriteLine("smtp = " + Properties.Settings.Default.SMTPAddress);
            //smtpClient.Port = 587;
            //smtpClient.EnableSsl = true;
            //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            ////smtpClient.UseDefaultCredentials = false;
            //smtpClient.Credentials = loginInfo;

            var smtpClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = true,
                Credentials = loginInfo,
                Timeout = 20000
            };

            //smtpClient.SendAsync(message, "test");
            smtpClient.Send(message);

            //Output to log
            OutputToLog(message);

            //Close Window
            Close();
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.ToString());
            MessageBox.Show("An error occurred.  Please see the trace log for more information.");
        }

I have tried everything mentioned in previous posts(there are many of them), but nothing seems to work. I have tried with and without default credentials and setting the flag to false/true. Nothing seems to work. Might anyone be able to point me in the right direction?

Thanks in advance!


Solution

  • Apparently GMAIL was blocking my email address from using less secure devices. You can turn off the blocking at: https://www.google.com/settings/security/lesssecureapps.