GMail API / OAUTH2 - 如何使用已弃用的传统 C# SMTP 方法发送电子邮件?

发布于 2025-01-12 13:13:25 字数 928 浏览 1 评论 0原文

根据 Google 的安全性较低的应用弃用通知的通知,自 2022 年 5 月 30 日起,通过“安全性较低的应用程序”访问 Google 帐户的做法已被弃用。下面的 C# 代码是我从我自己的个人 GMail 帐户发送电子邮件的方式。我只从我的应用程序发送电子邮件。我不会从此帐户执行任何其他操作(阅读电子邮件、创建草稿等...),仅发送。基本上,这相当于发送“不回复”电子邮件。

// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
    smtpClient.DeliveryMethod = SmtpDeliveryMethod;
    smtpClient.UseDefaultCredentials = UseDefaultCredentials;
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new NetworkCredential(SmtpUsername, SmtpPassword);

    MailMessage mailMessage = new MailMessage { /* ... */ };
    
    smtpClient.Send(mailMessage);
}

我已经开始查看 Google 的 API 和 OAuth 文档,但我觉得我正在陷入一个黑暗的兔子洞,因为我不想访问其他用户的数据,只想从我自己的帐户发送电子邮件。我从研究中假设 OAuth2 是尽可能做事的首选方式,但我找不到“如果你现在这样做”、“那么开始这样做”的明确示例。有人可以指出我更新代码所需路径的绝对起点,以便我可以继续从我自己的个人帐户/应用程序发送电子邮件吗?

As per notice from Google's Less Secure App Deprecation Notice, on May 30, 2022 the ability access Google accounts through 'less secure apps' is being deprecated. The C# code below is how I have been sending emails from my own personal GMail account. I only send emails from my app. I do not do anything else (read emails, create drafts, etc...) from this account, only sending. Basically, it is the equivalent of sending "no-reply" emails.

// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
    smtpClient.DeliveryMethod = SmtpDeliveryMethod;
    smtpClient.UseDefaultCredentials = UseDefaultCredentials;
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new NetworkCredential(SmtpUsername, SmtpPassword);

    MailMessage mailMessage = new MailMessage { /* ... */ };
    
    smtpClient.Send(mailMessage);
}

I've started looking at Google's API and OAuth documentation but I feel like I am going down a dark rabbit hole here since I am not wanting to access other users data, just only send emails from my own account. I assume from research that OAuth2 is the preferred way to do things when at all possible but I can't find a clear cut of example of "if you are doing it this way now", "then start doing this way". Can someone point me to the absolute starting point of the path I need to take to update my code so that I can continue sending emails from my own personal account/app?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2025-01-19 13:13:25

转到您的 Google 帐户并创建应用密码,创建后只需使用该密码即可您的 Gmail 帐户的实际密码。

安全性

在此处输入图像描述

// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
    smtpClient.DeliveryMethod = SmtpDeliveryMethod;
    smtpClient.UseDefaultCredentials = UseDefaultCredentials;
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new NetworkCredential(SmtpUsername, appsPasswrod);

    MailMessage mailMessage = new MailMessage { /* ... */ };
    
    smtpClient.Send(mailMessage);
}

Go to your google account and create an apps password once this is created just use that instead of the actual password for your gmail account.

Security

enter image description here

// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
    smtpClient.DeliveryMethod = SmtpDeliveryMethod;
    smtpClient.UseDefaultCredentials = UseDefaultCredentials;
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new NetworkCredential(SmtpUsername, appsPasswrod);

    MailMessage mailMessage = new MailMessage { /* ... */ };
    
    smtpClient.Send(mailMessage);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文