c# - 从 C# 应用程序发送邮件

标签 c# winforms email gmail visual-studio-2015

我正在尝试从我的 Windows 表单应用程序发送一封电子邮件。我看过很多类似的帖子,但似乎都不起作用。但是,当我更改 Gmail 中的设置以允许安全性较低的应用程序时,代码就可以工作了。但我不想让我的帐户容易受到此应用程序的攻击。这是如果我不允许安全性较低的应用程序时收到的代码和错误。

MailMessage mail = new MailMessage("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1a19183b1c161a121755181416" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d4544477d5a505c5451135e5250" rel="noreferrer noopener nofollow">[email protected]</a>", "Test Automation", "Did you receive this?");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabbb8b99abdb7bbb3b6f4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent", "success");

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

请帮忙!

附注:我使用 gmail 创建了一个 Outlook 帐户,当我输入 oulook 服务器和凭据时,代码可以正常工作。那么这是新的 Gmail 安全更改的问题吗?其他类似的问题已经解决了他们的问题,但在尝试了几乎所有解决方案后我仍然遇到相同的错误。

最佳答案

如果你没有 2 个因素

启用“安全性较低的应用程序”

https://www.google.com/settings/security/lesssecureapps

如果您有两步验证

您可以创建一个“应用程序密码”。转到下面的链接并添加一个自定义应用程序(只需编写您想要的任何名称,名称不重要,仅用于您自己的“簿记”),然后使用该密码作为密码。

client.Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ca8cb8a5a78aada7aba3a6e4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>", "Generated Password");

https://security.google.com/settings/u/1/security/apppasswords

注意: 如果您收到“您正在查找的设置不适用于您的帐户”,请使用“不太安全的应用程序”

关于c# - 从 C# 应用程序发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457668/

相关文章:

C# 组合框不显示项目

c# - 传递参数时部分 View 不显示

c# - 是什么触发了使用 SpaServices 在 HMR 中重建 webpack-bundle?

php - 如何使邮件形成工作

python - 使用 Python 中的 win32com 模块向多个收件人发送电子邮件

c# - 让 POST 端点在自托管 (WebServiceHost) C# web 服务中工作?

c# - 缩放 2D 图形 Pane 上的信号

c# - 组合框选定值

php - 使用 PHP IMAP 打开电子邮件

c# - LookUpEdit 中没有 DisplayMemberChanged