无法使用Gmail的应用程序密码从C#Win表单应用发送电子邮件

发布于 2025-02-06 10:31:51 字数 1102 浏览 1 评论 0原文

我们曾经发送带有不太安全的应用程序选项的电子邮件,这是不再可能的。我已经设置了两种身份验证,我创建了一个应用程序密码,但是当我尝试发送带有应用程序密码的电子邮件而不是帐户密码时,我会收到错误:

System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.IO.IOException: Unable to read data from the transport connection: The connection was closed.

发送的代码为:

Smtp = new SmtpClient();
Smtp.UseDefaultCredentials = false;
var NetworkCredentials = new NetworkCredential() { UserName = "[email protected]", Password = "..." };
Smtp.Port = 587;
Smtp.EnableSsl = true;
Smtp.Host = "smtp.gmail.com";
Smtp.Credentials = NetworkCredentials;
    
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = "test";
Smtp.Send(msg);

We used to send emails with the Less secure apps option, which is no longer possible. I have setup the 2 way authentication, I have created an app password, but when I try to send an email with the app password instead of the account password I get the error :

System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.IO.IOException: Unable to read data from the transport connection: The connection was closed.

The code for sending is :

Smtp = new SmtpClient();
Smtp.UseDefaultCredentials = false;
var NetworkCredentials = new NetworkCredential() { UserName = "[email protected]", Password = "..." };
Smtp.Port = 587;
Smtp.EnableSsl = true;
Smtp.Host = "smtp.gmail.com";
Smtp.Credentials = NetworkCredentials;
    
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = "test";
Smtp.Send(msg);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文