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("from...@gmail.com"), new MailAddress("to...@gmail.com"));
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!
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="xxxxxxx@gmail.com" 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.
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, xxxxx@gmail.com. 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.
It seems your connection is rejected by the SMTP server either because it is not SSL enabled or the credentials are incorrect. You would need to setup SSL and network credentials in your web.config as below:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network enableSsl="true" host="smtp.gmail.com" port="25" userName="xxxxxxx@gmail.com" password="xxxxxxxxxxx"/>
</smtp>
</mailSettings>
</system.net>
More info is available in this SO discussion:
C# - Can't send mail in WIndows Azure via Gmail SMTP
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 said before your username should contain "@googlemail.com". In my code (java) I'm using port 465 to sent mails through google mail.