C# System.Net.Mail.SmtpException: SMTP 服务器需要安全连接或客户端未通过身份验证

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

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated

c#smtpsmtpclient

提问by Adrian

I'm trying to send email with my website's address from a C# application.
This worked fine for several months until recently. (maybe my provider changes some things or someone else changed settings)

我正在尝试从 C# 应用程序发送带有我网站地址的电子邮件。
直到最近,这几个月都运行良好。(也许我的提供商更改了某些内容或其他人更改了设置)

Here's the code:

这是代码:

  private void sendEmail(Email invite) {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smtpServerName);
            mail.From = new MailAddress(emailUsername);

            mail.To.Add(invite.RecipientEmail);
            mail.Subject = invite.MessageSubject;
            mail.Body = invite.MessageBody;

            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);
//          SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }  

Here's the error:

这是错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:需要 SMTP 身份验证。

Looking at other questions I tried what they suggested, to make SmtpServer.EnableSsl = true. This didn't work at all. It gave the following:

看着其他问题,我尝试了他们的建议,使SmtpServer.EnableSsl = true. 这根本不起作用。它给出了以下内容:

System.Net.Mail.SmtpException: Server does not support secure connections.

System.Net.Mail.SmtpException: 服务器不支持安全连接。

I'm guessing I should disable SSL and have it the way it was before.

我猜我应该禁用 SSL 并像以前一样使用它。

Any suggestions how to make email sending work again?

任何建议如何使电子邮件发送再次工作?

EDIT
I've tried without SmtpServer.UseDefaultCredentials = false;
I've tried with it set to true: SmtpServer.UseDefaultCredentials =true;
I've tried commenting that line along with the following //SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);

编辑
我试过但SmtpServer.UseDefaultCredentials = false;
我试过将它设置为true:SmtpServer.UseDefaultCredentials =true;
我试过评论该行以及以下内容//SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);

采纳答案by Reed Copsey

That error message is typically caused by one of the following:

该错误消息通常是由以下原因之一引起的:

  • Incorrect connection settings, such as the wrong port specified for the secured or non-secured connection
  • Incorrect credentials. I would verify the username and password combination, to make sure the credentials are correct.
  • 连接设置不正确,例如为安全或非安全连接指定了错误的端口
  • 凭据不正确。我会验证用户名和密码组合,以确保凭据正确。

回答by Ravindra Bagale

I think you have to set DeliveryMethod = SmtpDeliveryMethod.Network

我想你必须设置 DeliveryMethod = SmtpDeliveryMethod.Network

this one is currently working in my PC, just i checked,working nice,try this

这个目前正在我的电脑上工作,我刚刚检查过,工作得很好,试试这个

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

回答by Anitha Neelavathi

If you are sure that your Username and Password are correct, but you still get errors then it means that Gmail has blocked your application.

如果您确定您的用户名和密码正确,但仍然出现错误,则表示 Gmail 已阻止您的应用程序。

You should check 'Turn on' radio button in the following link relevant to your gmail account. It would then work fine.

您应该在以下与您的 Gmail 帐户相关的链接中选中“打开”单选按钮。然后它会正常工作。