Search code examples
.netazuresmtpgmail

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!


Solution

  • 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.