C# 使用 gmail 发送电子邮件。错误:服务器响应为:5.5.1 需要身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29958229/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:24:10  来源:igfitidea点击:

Send email using gmail. Error: The server response was: 5.5.1 Authentication Required

c#asp.netemail

提问by ideas xchange

Sorry, I saw a lot of similar post regarding this matter but never found any solution to my problem, so I decided to post it.

抱歉,我看到很多关于这个问题的类似帖子,但从未找到任何解决我问题的方法,所以我决定发布它。

I am using ASP.NET c# to send email programmatically using gmail with the following code.

我正在使用 ASP.NET c# 通过以下代码使用 gmail 以编程方式发送电子邮件。

 string EmailAddress = senderemail;
    MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
    mailMessage.Subject = "This is a test email";
    mailMessage.Body = "This is a test email. Please reply if you receive it.";

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 587;

    smtpClient.Credentials = new System.Net.NetworkCredential()
    {
        UserName = EmailAddress,
        Password = senderpassword
    };
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Send(mailMessage);

I received this error like everybody else

我和其他人一样收到了这个错误

Error:

错误:

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.

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多信息。

Already did below 2 action from GMAIL.

已经从 GMAIL 执行了以下 2 个操作。

-less secure app : turn on

- 不太安全的应用程序:打开

-2 step verification : off

-2 步验证:关闭

I don't care whether is this gmail account safe or what. I don't need any security for this account. What else should i do?

我不在乎这个 gmail 帐户是否安全或什么。我不需要此帐户的任何安全措施。我还应该做什么?

回答by ideas xchange

I ran this code in my machine,it works. Probably Commented Line Not needed.....

我在我的机器上运行了这段代码,它有效。可能注释行 不需要.....

MailMessage mailMessage = new MailMessage("From_User_Email", "To_Whom_Email");
        mailMessage.Subject = "This is a test email";
        mailMessage.Body = "This is a test email. Please reply if you receive it.";

        SmtpClient smtpClient = new SmtpClient();
        //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;

        smtpClient.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "From_User_Email",
            Password = "User_Password"
        };
        //smtpClient.UseDefaultCredentials = false;
        smtpClient.Send(mailMessage);

回答by Tharif

Try the following code :

试试下面的代码:

UseDefaultCredentials = false;
Credentials = new NetworkCredential("[email protected]", "xxxpassword");

When you try to send mail from code and you find the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required", than the error might occur due to the cases which are beutifully explained here,please have a look

当您尝试从代码发送邮件并发现错误“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证”时,由于以下情况,可能会发生错误在这里解释得很清楚,请看一看

Also check blocked sign ins here,security devices setting here

还要检查受阻迹象插件在这里,安全设备设置在这里

Also you can try the below code :

您也可以尝试以下代码:

 MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("[email protected]");

                mail.To.Add("[email protected]");
                mail.Subject = "xxxxx ";
                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>xxxxxx</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("[email protected]", "xxxpassword");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

Thanks

谢谢

回答by Krunal Mevada

This generally happens when you try login from different time zone/IP/Computer. You production server and the mail id you have used both are in different time zone. Choose any of the one solution:

当您尝试从不同的时区/IP/计算机登录时,通常会发生这种情况。您的生产服务器和您使用的邮件 ID 位于不同的时区。选择以下任一解决方案:

  1. Log in to production server via remote access, and sing in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.
  1. 通过远程访问登录到生产服务器,并使用您的凭据登录到 gmail 一次。他们将要求确认,确认并注销。

Or

或者

  1. Log in gmail to your local computer, Follow this Linkand choose review this activityand take proper actions.
  1. 将 gmail 登录到您的本地计算机,点击此链接并选择查看此活动并采取适当的措施。

回答by Hussein Zawawi

Login to the following gmail account using a browser and follow the validation process. You may have sent a lot of emails and the account was blocked temporary.

使用浏览器登录以下 Gmail 帐户并按照验证过程进行操作。您可能已经发送了很多电子邮件,并且该帐户被临时阻止。

回答by mqueirozcorreia

Make sure you have a strong password. I had the problem with gmail because my password had only letters and numbers. I changed to use Upper and lower case letters, numbers and symbols, now it is working: "Using numbers, symbols and mix of upper and lower case letters in your password makes it harder for someone to guess your password. For example, an eight-character password with numbers, symbols and mixed-case letters is harder to guess because it has 30,000 times as many possible combinations than an eight-character password with only lower case letters." https://support.google.com/accounts/answer/32040?hl=en

确保你有一个强密码。我在使用 gmail 时遇到了问题,因为我的密码只有字母和数字。我改为使用大写和小写字母、数字和符号,现在它可以工作了:“在密码中使用数字、符号和大小写字母的混合会使别人更难猜出你的密码。例如,一个 8 - 包含数字、符号和混合大小写字母的字符密码更难猜测,因为它的可能组合数是仅包含小写字母的八字符密码的 30,000 倍。” https://support.google.com/accounts/answer/32040?hl=en

回答by Kutty Rajesh Valangai

sign up Gmail and go to https://www.google.com/settings/security/lesssecureappswhere you can see settings. Access for less secure apps

注册 Gmail 并转到https://www.google.com/settings/security/lesssecureapps,您可以在其中查看设置。访问不太安全的应用程序

Turn off (default) ==> Turn On

关闭(默认)==> 打开

after add this code,

添加此代码后,

  MailMessage mail = new MailMessage("[email protected]", "[email protected]");
                mail.Subject = "TestEmailImportant";
                mail.Body = "This mail is to test if this program is working";

                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "[email protected]",
                    Password = "YYYYYYYY"
                };

                smtpClient.EnableSsl = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                        System.Security.Cryptography.X509Certificates.X509Chain chain,
                        System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };

                smtpClient.Send(mail);

回答by Batsuuri .G

My case was solved this this solution6

我的情况解决了这个解决方案6

Commit the following line:

提交以下行:

smtpClient.UseDefaultCredentials = false;

If it not working try port 25 instead of 578.

如果它不起作用,请尝试使用端口 25 而不是 578。

https://productforums.google.com/forum/#!topic/gmail/w8hNo56tivE;context-place=topicsearchin/gmail/The$20SMTP$20server$20requires$20a$20secure$20connection$20or$20the$20client$20was$20not$20authenticated.$20The$20server$20response$20was$3A$205.5.1$20Authentication$20Required

https://productforums.google.com/forum/#!topic/gmail/w8hNo56tivE;context-place=topicsearchin/gmail/$20SMTP$20server$20requires$20a$20secure$20connection$20或$20the$20client$20was$20不是$20authenticated。$20The$20server$20response$20was$3A$205.5.1$20Authentication$20Required

回答by Mostafa Kamel

Some times you only need to login gmail from this computer , if you did not login before from this PC . gmail will ask you to verify and confirm the new PC.

有时您只需要从这台计算机登录 gmail,如果您之前没有从这台 PC 登录。gmail 会要求您验证并确认新 PC。

回答by Sukanya Bhavanibatla