0

I am running an ASP.Net application on windows azure and I need to send an email from it. I am trying to send it using my gmail account as follows, however when I attempt to send it, I get the following 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 code:

        private static readonly string EMAIL_ADDRESS = "mymail@gmail.com";
        private static readonly string EMAIL_FROM = "MY FROM NAME";
        private static readonly string EMAIL_PASS = "my Password";
        private static readonly string EMAIL_HOST = "smtp.gmail.com";
        private static readonly int EMAIL_PORT = 587;
        private static readonly bool EMAIL_SSL = true;
        private static readonly SmtpDeliveryMethod EMAIL_DELIVERY_METHOD = SmtpDeliveryMethod.Network;
        private static readonly bool EMAIL_DEFAULT_CREDENTIALS = false;


        public static void SendEmail(string recipientEmail, string subject, string body) {
            var fromAddress = new MailAddress(EMAIL_ADDRESS, EMAIL_FROM);
            var toAddress = new MailAddress(recipientEmail, "MY CLIENT NAME");

            var smtp = new SmtpClient {
                Host = EMAIL_HOST,
                Port = EMAIL_PORT,
                EnableSsl = EMAIL_SSL,
                DeliveryMethod = EMAIL_DELIVERY_METHOD,
                UseDefaultCredentials = EMAIL_DEFAULT_CREDENTIALS,
                Credentials = new NetworkCredential(fromAddress.Address, EMAIL_PASS,EMAIL_HOST)
            };
            using (var message = new MailMessage(fromAddress, toAddress) {
                Subject = subject,
                Body = body,
                Priority = MailPriority.High
            }) {
                smtp.Send(message);
            }
        }

I have enabled Less Secure Sign-in, and the configuration seems to be fine. What is the problem?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
mangusbrother
  • 3,988
  • 11
  • 51
  • 103
  • Try to leave the third parameter to networkcredentials empty and maybe look at sendgrid or similar for use with azure – jakobandersen Apr 12 '15 at 19:45
  • I have tried without the 3rd parameter, same result. And I'd rather keep to this if possible. This should work from what I've seen. – mangusbrother Apr 12 '15 at 20:56
  • Maybe a duplicate of this: http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not if you are in a different timezone than Azure datacenter – jakobandersen Apr 13 '15 at 11:35
  • i have checked the google settings, and found the entry from before I enabled less secure apps, however now (after enabling less secure apps) whenever I try to send it I do not get any new alerts. – mangusbrother Apr 13 '15 at 12:02

3 Answers3

1

Try to remove the UseDefaultCredentials property.

If that doesn't work, set the properties individually (not using the object initialiser) and make sure Credentials is set after UseDefaultCredentials

see c# SmtpClient class not able to send email using gmail

Community
  • 1
  • 1
ajg
  • 1,743
  • 12
  • 14
1

I know this answere is a bit late, but You are using a NOT SECURE WAY to login to your gmail. Gmail actually throws an error but this is not send to you. You may receive an email that says something like:

Someone tried to connect to you gmail account from an unsecure application

Or some similar. In this case you have to visit

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

(Provided you are logged in)

and set this option to allow unsecure apps. Than you are allowed to send mails (even in this unsecure way).

Having the same problem atm but I dont want to allow unsecure apps, I want to change my code to be secure. But cant find anything that solves this.

Dwza
  • 6,494
  • 6
  • 41
  • 73
0

Your code looks fine. Did you check that the email address hasn't been locked out? I.e. actually login to that email account on your browser. It happened to me before where the gmail account I was using got locked.

KDizzle
  • 51
  • 6