Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use Gmail smtp from Azure Cloud Service

My code for sending email through Gmail's smtp:

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my_user_name", "my_password");

MailMessage message =
     new MailMessage(new MailAddress("[email protected]"), new MailAddress("[email protected]"));
message.Body = "body";
message.Subject = "subject";
client.Send(message);

The code works on my local machine and when I publish at Azure as "Web Site".

BUT when I publish in a "Cloud Service" I get this exception:

 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

Is there anything that differ a Windows Azure "Web site" from a "Cloud Service" that could have this effect?

Thanks!

like image 699
Cotten Avatar asked Jun 13 '12 15:06

Cotten


People also ask

Can I use SMTP on Azure?

With SocketLabs Azure SMTP server, you can integrate email sending capabilities into your Azure service fast, so you can start sending emails from Azure. Microsoft Azure does not offer Azure email servers out of the box and sending email from Windows Azure presents dozens of deliverability challenges.

Does Gmail still support SMTP?

For non-Gmail clients, Gmail supports the standard IMAP, POP, and SMTP protocols. The Gmail IMAP, POP, and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol.


3 Answers

I experienced this exact problem. However, I experienced the problem regardless of the fact that I was using the <system.net> configuration settings and I was using the proper credentials, host, port, etc.

The problem was that Google was rejecting the authentication request that was coming from Azure. I found this out by logging into the Gmail account that I was using for the SMTP Client in my code. Once I logged into the Gmail account, I noticed a red-bar-header-warning that said

Someone signed in from a location that isn't typical for your account. If it wasn't you, change your password immediately.

in addition to the warning, I received an email that said:

Someone recently tried to use an application to sign in to your Google Account, [email protected]. We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:

  • Monday, August 27, 2012 10:33:59 PM GMT
  • IP Address: 168.62.48.183
  • Location: United States

If you do not recognize this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately. Find out how at http://support.google.com/accounts?p=reset_pw

If this was you, and you want to give this application access to your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login

Sincerely, The Google Accounts Team

After I followed the steps listed in the provided link, my Azure Website was able to successfully log into my Gmail account and use Gmail as the SMTP Client.

like image 81
Jed Avatar answered Oct 04 '22 20:10

Jed


Use following SMTP settings in Web.config:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" userName="[email protected]" password="xxxxxxxxxxx"/>
        </smtp>
    </mailSettings>
</system.net>

I think you are passing wrong credentials. Use @gmail.com suffix in your user name and try to set bodyhtml property true also...

Hope this will work for you.. It always work correctly to me..

Check answer's comment in the this SO thread.

like image 32
Niranjan Singh Avatar answered Oct 04 '22 19:10

Niranjan Singh


As instructed on the Google troubleshooting page, going to the following link and logging in from my local computer fixed the error when sending email from an Azure website for me.

http://www.google.com/accounts/DisplayUnlockCaptcha

like image 8
chief7 Avatar answered Oct 04 '22 19:10

chief7