C# 无法通过 MailMessage 和 smtpClient 对 Gmail smtp 进行身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9104645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't auth to Gmail smtp via MailMessage & smtpClient
提问by PositiveGuy
I cannot figure out for the life of my why this isn't working
我终生无法弄清楚为什么这不起作用
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere"),
EnableSsl = true,
Timeout = 10000
};
smtp.Send(mail);
I get:
我得到:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。
I just specified EnableSsl to true so that shouldn't be the issue in terms of secure connection.
我只是将 EnableSsl 指定为 true,因此这不应该是安全连接方面的问题。
I'm running this from localhost. And yes, my username and password I'm entering to auth (my gmail account credentials) is 100% right.
我正在从本地主机运行它。是的,我输入的用户名和密码(我的 gmail 帐户凭据)是 100% 正确的。
回答by coder
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com" password="yourpassword" port="587" userName="username"/>
</smtp>
</mailSettings>
Edit: try adding this line smtp.Credentials = Credentialsafter this
编辑:尝试smtp.Credentials = Credentials在此之后添加此行
Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere"),
Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere"),
回答by ukhardy
Have a callback as follows. Tell System.Netto ignore the error!
有一个回调如下。告诉System.Net忽略错误!
Add this before call to Send()
在调用之前添加这个 Send()
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtp.Send(mail);
回答by krishna
This worked just fine for me
这对我来说很好用
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("[email protected]", "mypassword"),
EnableSsl = true,
Timeout = 10000
};
MailMessage message = new MailMessage();
message.Body = "hello there";
message.Subject = "hi!!";
message.To.Add("[email protected]");
message.From = new MailAddress("[email protected]");
smtp.Send(message);
回答by Aleh Zasypkin
It looks like Gmail requires Application-specific password(not your main password).
看起来 Gmail 需要特定于应用程序的密码(不是您的主密码)。
Please, look into this: http://support.google.com/mail/bin/answer.py?hl=en&answer=1173270
请查看此内容:http: //support.google.com/mail/bin/answer.py?hl=zh-CN&answer=1173270
I had the same problem recently.
我最近遇到了同样的问题。
回答by edteke
If login info is 100% right, you need to set UseDefaultCredentials = falsefirstand then set the credentials you want to use Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere").
如果登录信息是100%正确的,你需要设置UseDefaultCredentials = false第一,然后设置要使用的凭据Credentials = new NetworkCredential("[email protected]", "myGmailPasswordHere")。
If you set the credentials first, when you set UseDefaultCredentials = falsethis will make the Credentialsproperty to null.
如果您先设置凭据,则在设置时UseDefaultCredentials = false会将Credentials属性设为null.
This is wired, but it happened to me.
这是有线的,但它发生在我身上。
Debug your code and check if the Credentialsproperty is null before you call smtp.Send(message);. If so, then try inverting the order. It seems you have it in the right order, but if it's null, don't use the inline initialization.
Credentials在调用之前调试您的代码并检查该属性是否为空smtp.Send(message);。如果是这样,那么尝试颠倒顺序。看起来您的顺序正确,但如果它为空,请不要使用内联初始化。
Hope it helps.
希望能帮助到你。
EDIT:If you are using two-step verification, be sure you are using an App Specific password
编辑:如果您使用两步验证,请确保您使用的是应用专用密码
回答by oliver
Had the same issue accessing smtp.gmail.com from an ASP.NET application running on Amazon AWS hosting. It turned out that my configuration was right - it was also working fine from another computer - but gmail would deny my login attempt, because I try logging in from an unusual location. I logged on to gmail over the web interface (www.gmail.com), and had to answer a captcha. After that it worked.
从 Amazon AWS 托管上运行的 ASP.NET 应用程序访问 smtp.gmail.com 时遇到了同样的问题。结果证明我的配置是正确的——它在另一台计算机上也能正常工作——但 gmail 会拒绝我的登录尝试,因为我尝试从一个不寻常的位置登录。我通过网络界面 (www.gmail.com) 登录到 gmail,并且不得不回答验证码。在那之后它起作用了。
回答by Tiny
I know this is an old topic, BUT... Google has changed something on their security settings.
我知道这是一个老话题,但是......谷歌已经改变了他们的安全设置。
Was struggling with all the answers until I checked my email with a mail from Google stating that "we've recently blocked a sign-in attempt on your Google account".
一直在努力寻找所有答案,直到我用来自 Google 的邮件检查了我的电子邮件,上面写着“我们最近阻止了对您的 Google 帐户的登录尝试”。
That led me to this page: Google Account Security
这让我进入了这个页面: Google 帐户安全
Under the "Access for less secure apps" section, you can enable access to your account from other devices/applications... like your C# application.
在“访问不太安全的应用程序”部分下,您可以启用从其他设备/应用程序访问您的帐户...例如您的 C# 应用程序。
Note, there is no longer an "application specific" section.
请注意,不再有“特定于应用程序”的部分。
Hope this helps someone... I just lost 30 minutes of my life...
希望这对某人有所帮助...我刚刚失去了生命中的 30 分钟...
回答by Robert
My problem was that the domain-owner for our gmail-account had disabled both "Access for less secure apps" and two step authentication. There was no way to enable it, I couldn't even see the setting. So I tested with my personal account instead, and it worked fine.
我的问题是我们的 gmail 帐户的域所有者禁用了“访问不太安全的应用程序”和两步身份验证。没有办法启用它,我什至看不到设置。所以我改用我的个人帐户进行了测试,效果很好。
回答by rktuxyn
Very simple just follow this for C# WPF Application:
非常简单,只需按照 C# WPF 应用程序执行此操作即可:
private void SendByGmail(string subject, string body, string recepientEmail, string MailMsgFrom, string MailMsgPass)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(MailMsgFrom);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
mailMessage.Priority = System.Net.Mail.MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Timeout = 200000;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = MailMsgFrom;
NetworkCred.Password = MailMsgPass;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage);
}
}
After that you should get like this Error
之后你应该得到这样的错误
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"} System.Net.Mail.SmtpException
To Solve this problem, at first login your email account to your google account in web browser. Then just follow this link Google Account Activity. Then you'll get recent Devices & activity by your account. If show block your current activity from your current device. Just Unblock this. Then try again to send email.
Thanks
要解决此问题,首先在网络浏览器中将您的电子邮件帐户登录到您的 google 帐户。然后只需点击此链接Google 帐户活动。然后,您将通过您的帐户获得最近的设备和活动。如果显示阻止您当前设备的当前活动。只是取消阻止这个。然后再次尝试发送电子邮件。
谢谢
回答by Abdulhameed
I had this problem before and fixed it by following these steps:
我之前遇到过这个问题,并按照以下步骤修复了它:
- Go to "My Account" settings.
- Click "Sign-in & Security" category.
- Scroll down to "Connected apps & sites" section.
- turn off the "Allow less secure apps" option.
- 转到“我的帐户”设置。
- 单击“登录和安全”类别。
- 向下滚动到“连接的应用程序和网站”部分。
- 关闭“允许不太安全的应用程序”选项。
I just turned this option off and my code ran successfully.
我刚刚关闭了这个选项,我的代码运行成功。

