Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

.net - 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("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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...