C# SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18503333/
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
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?
提问by Abhay Andhariya
I want to send an email from my application and i have written following code for sending mail
我想从我的应用程序发送一封电子邮件,我已经编写了以下用于发送邮件的代码
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mymailid");
msg.To.Add("receipientid");
msg.Subject = "test";
msg.Body = "Test Content";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(msg);
I am running it on localhost so what mistake i am doing to send it.
我在本地主机上运行它,所以我在发送它时犯了什么错误。
When i send button it gives an error like
当我发送按钮时,它会出现类似的错误
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 需要身份验证。
Code in Web.config file
Web.config 文件中的代码
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.gmail.com" />
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="smtpUser" value="[email protected]" />
<add key="smtpPass" value="mypassword" />
<add key="adminEmail" value="[email protected]" />
</appSettings>
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com" password="mypassword" port="587" userName="[email protected]" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
what should i do to solve this error and send mail??
我应该怎么做才能解决这个错误并发送邮件?
采纳答案by Ronak Patel
First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below
首先检查 gmail 的安全相关问题。您可能在 gmail 中启用了双重身份验证。如果您收到任何安全警报,还要检查您的 Gmail 收件箱。在这种情况下,请检查@mjb 的其他答案,如下所示
Below is the very general thing that i always check first for such issues
以下是我总是首先检查此类问题的非常普遍的事情
client.UseDefaultCredentials = true;
set it to false.
将其设置为假。
Note @Joe King's answer- you must set client.UseDefaultCredentials beforeyou set client.Credentials
注意@Joe国王的答案-你必须设置client.UseDefaultCredentials之前你设定client.Credentials
回答by meda
Try it this way, I just made some light changes:
试试这种方式,我只是做了一些轻微的改变:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = "test";
msg.Body = "Test Content";
//msg.Priority = MailPriority.High;
using (SmtpClient client = new SmtpClient())
{
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "mypassword");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(msg);
}
Also please show your app.config file, if you have mail settings there.
如果你有邮件设置,也请显示你的 app.config 文件。
回答by Shuchita Bora
Try to login in your gmail account. it gets locked if you send emails by using gmail SMTP. I don't know the limit of emails you can send before it gets locked but if you login one time then it works again from code. make sure your webconfig setting are good.
尝试登录您的 Gmail 帐户。如果您使用 gmail SMTP 发送电子邮件,它会被锁定。我不知道在被锁定之前您可以发送的电子邮件的限制,但是如果您登录一次,则它可以通过代码再次运行。确保您的 webconfig 设置良好。
回答by Ganesh Pillai N
Below is my code.I also had the same error but the problem was that i gave my password wrong.The below code will work perfectly..try it
下面是我的代码。我也有同样的错误,但问题是我给我的密码错误。下面的代码将完美运行..试试看
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Password Recovery ";
mail.Body += " <html>";
mail.Body += "<body>";
mail.Body += "<table>";
mail.Body += "<tr>";
mail.Body += "<td>User Name : </td><td> HAi </td>";
mail.Body += "</tr>";
mail.Body += "<tr>";
mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
mail.Body += "</tr>";
mail.Body += "</table>";
mail.Body += "</body>";
mail.Body += "</html>";
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
You can reffer it in Sending mail
您可以在发送邮件中重新提交
回答by Soros Liu
I encountered the same problem even I set "UseDefaultCredentials" to false. Later I found that the root cause is that I turned on "2-step Verification" in my account. After I turned it off, the problem is gone.
即使我将“UseDefaultCredentials”设置为 false,我也遇到了同样的问题。后来我发现根本原因是我在我的帐户中打开了“两步验证”。我关掉之后,问题就解决了。
回答by mjb
I have the same problem.
我也有同样的问题。
I have found this solution:
我找到了这个解决方案:
Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.
Google 可能会阻止来自某些不使用现代安全标准的应用程序或设备的登录尝试。由于这些应用程序和设备更容易被入侵,因此阻止它们有助于确保您的帐户更安全。
Some examples of apps that do not support the latest security standards include:
一些不支持最新安全标准的应用程序示例包括:
- The Mail app on your iPhone or iPad with iOS 6 or below
- The Mail app on your Windows phone preceding the 8.1 release
- Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
- 装有 iOS 6 或更低版本的 iPhone 或 iPad 上的邮件应用
- 8.1 版本之前 Windows 手机上的邮件应用程序
- 一些桌面邮件客户端,如 Microsoft Outlook 和 Mozilla Thunderbird
Therefore, you have to enable Less Secure Sign-In(or Less secure app access) in your google account.
因此,您必须在您的 Google 帐户中启用安全性较低的登录(或安全性较低的应用程序访问)。
After sign into google account, go to:
登录谷歌账户后,前往:
https://www.google.com/settings/security/lesssecureapps
or
https://myaccount.google.com/lesssecureapps
https://www.google.com/settings/security/lesssecureapps
或
https://myaccount.google.com/lesssecureapps
In C#, you can use the following code:
在 C# 中,您可以使用以下代码:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
-------------------
Info shared by Michael Freidgeim in below comments area:
Similar answer with screenshots https://stackoverflow.com/a/32457468/52277
-------------------
Michael Freidgeim 在下面的评论区分享的信息:
类似的答案截图https://stackoverflow.com/a/32457468/52277
回答by Joe King
Ensure you set SmtpClient.Credentials
aftercalling SmtpClient.UseDefaultCredentials = false
.
确保SmtpClient.Credentials
在调用后进行设置SmtpClient.UseDefaultCredentials = false
。
The order is important as setting SmtpClient.UseDefaultCredentials = false
will reset SmtpClient.Credentials
to null.
顺序很重要,因为设置SmtpClient.UseDefaultCredentials = false
将重置SmtpClient.Credentials
为 null。
回答by Yakir Manor
some smtp servers (secure ones) requires you to supply both username and email, if its gmail then most chances its the 'Less Secure Sign-In' issue you need to address, otherwise you can try:
某些 smtp 服务器(安全服务器)要求您同时提供用户名和电子邮件,如果是 gmail,则很有可能是您需要解决的“安全性较低的登录”问题,否则您可以尝试:
public static void SendEmail(string address, string subject,
string message, string email, string username, string password,
string smtp, int port)
{
var loginInfo = new NetworkCredential(username, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient(smtp, port);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
notice that the email from and the username are different unlike some implementation that refers to them as the same.
请注意,电子邮件来自和用户名是不同的,这与某些将它们称为相同的实现不同。
calling this method can be done like so:
可以像这样调用这个方法:
SendEmail("[email protected]", "test", "Hi it worked!!",
"from-mail", "from-username", "from-password", "smtp", 587);
回答by Shivam Srivastava
Make sure that Access less secure app is allowed.
确保允许访问不太安全的应用程序。
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.Sender = new MailAddress("[email protected]");
mail.To.Add("external@emailaddress");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Body content from";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xx");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Timeout = 30000;
try
{
smtp.Send(mail);
}
catch (SmtpException e)
{
textBox1.Text= e.Message;
}
回答by Ezequiel Santana
If it's a new google account, you have to send an email (the first one) through the regular user interface. After that you can use your application/robot to send messages.
如果是新的 google 帐户,则必须通过常规用户界面发送电子邮件(第一个)。之后,您可以使用您的应用程序/机器人发送消息。