gpt4 book ai didi

c# - 使用 gmail 发送电子邮件。错误 : The server response was: 5. 5.1 需要身份验证

转载 作者:行者123 更新时间:2023-11-30 18:52:24 24 4
gpt4 key购买 nike

对不起,我看到了很多关于这个问题的类似帖子,但没有找到解决我的问题的方法,所以我决定发布它。

我正在使用 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);

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

错误:

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.

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

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

-2步验证:关闭

我不关心这个 gmail 帐户是否安全。我不需要此帐户的任何安全性。我还应该做什么?

最佳答案

注册 Gmail 并转到 https://www.google.com/settings/security/lesssecureapps在哪里可以看到设置。访问安全性较低的应用

关闭(默认)==> 打开

添加这段代码后,

  MailMessage mail = new MailMessage("fromm@gmail.com", "toaddress@gmail.com");
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 = "XXXXXX@gmail.com",
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);

关于c# - 使用 gmail 发送电子邮件。错误 : The server response was: 5. 5.1 需要身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com