C# SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 Authentication Required
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10698779/
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 Sanat Pandey
I have a Problem, when i m sending a mail through this code then error is occured that "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 需要身份验证”的错误。
and my code is :
我的代码是:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
string body = "<table><tr><td>Company Name:</td><td>" + txt_cname.Text + "</td></tr><tr><td>Address With No.:</td><td>" + txt_addwithno.Text + "</td></tr><tr><td>Contact Person:</td><td>" + txt_conperson.Text + "</td></tr><tr><td>Email Id</td><td>" + txt_email.Text + "</td></tr><tr><td>Description</td><td>" + txt_description.Text + "</td></tr></table>";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xyz");
smtp.EnableSsl = true;
smtp.Send(mail);
txt_cname.Focus();
txt_cname.Text = "";
txt_addwithno.Text = "";
txt_conperson.Text = "";
txt_email.Text = "";
txt_description.Text = "";
}
回答by Anders Lindahl
I think you should set UseDefaultCredentialsto false, since you define credentials for the connection.
我认为您应该将UseDefaultCredentials设置为 false,因为您为连接定义了凭据。
Set this property to truewhen this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user.
[...]
If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be usedfor the credentials when connecting to the server
当此 SmtpClient 对象应(如果服务器请求)使用当前登录用户的默认凭据进行身份验证时,将此属性设置为true。
[...]
如果 UseDefaultCredentials 属性设置为false,则Credentials 属性中设置的值将用于连接到服务器时的凭据
回答by nWorx
i think you are using the wrong port.
我认为您使用了错误的端口。
you should use 465
你应该使用 465
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
http://support.google.com/mail/bin/answer.py?hl=zh-CN&answer=13287
cheers
干杯
回答by Vipin Nair
Try the port 587(it worked for me ) and also set UseDefaultCredentials to false. Set this and try to send the mail
尝试端口 587(它对我有用)并将 UseDefaultCredentials 设置为 false。设置这个并尝试发送邮件
回答by Sergej Popov
Google may prevent an application from accessing your account
Here you can enable applications to access google account with your credentials:
Google 可能会阻止应用程序访问您的帐户
您可以在此处启用应用程序以使用您的凭据访问 Google 帐户:
回答by George
The port doesn't really matter as System.Net.Mail does not support implicit SSL, in other words it totally ignores the "Port" parameter.
端口并不重要,因为 System.Net.Mail 不支持隐式 SSL,换句话说,它完全忽略了“端口”参数。
回答by Mac
I was trying to set this up through SQL Server 2012 Database Mail, and was getting the same message.
我试图通过 SQL Server 2012 数据库邮件进行设置,并收到相同的消息。
I was never able to get it to work using my primary g-mail account, which uses double authentication, even after using an Application-specific password. I ended up trying my secondary account, which does not have double auth, and it worked immediately. So for anyone who can't get it working using one of the solutions above, try it with a standard auth g-mail account.
即使使用特定于应用程序的密码,我也无法使用我的主 g-mail 帐户使其工作,该帐户使用双重身份验证。我最终尝试了我的辅助帐户,该帐户没有双重身份验证,并且立即生效。因此,对于使用上述解决方案之一无法使其正常工作的任何人,请使用标准的 auth g-mail 帐户尝试它。
回答by Anas Naguib
Microsoft States that SmtpClient is not recommended. You should use MailKit it will work for sure.
Microsoft 声明不推荐使用 SmtpClient。你应该使用 MailKit 它肯定会工作。
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.0
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.0
We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.
我们不建议您将 SmtpClient 类用于新开发,因为 SmtpClient 不支持许多现代协议。请改用 MailKit 或其他库。有关更多信息,请参阅 GitHub 上不应使用 SmtpClient。

