[英]How do I send an email from my C# windows application?
Im working on a Windows app where the user has to enter a password.我正在开发一个 Windows 应用程序,用户必须在其中输入密码。 I also have a "Forgot Password" link.
我还有一个“忘记密码”链接。 on the window. When that's clicked, I have the user enter their email address and click a Submit button.
在 window 上。单击后,我让用户输入他们的 email 地址,然后单击“提交”按钮。 Every time they enter an email address and click the button, I get the error message:
每次他们输入 email 地址并单击按钮时,我都会收到错误消息:
SmtpException has occured: The SMTP server requires a secure connection or the client was not authenticated.
发生 SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.7.0 Authentication Required.
服务器响应为:5.7.0 需要身份验证。
The code I'm using is:我正在使用的代码是:
try
{
Cursor = Cursors.WaitCursor;
MailAddress from = new MailAddress("bgatt64@gmail.com", "Bob Gatto");
MailAddress to = new MailAddress("bgatto64@yahoo.com", "Bob Gatto");
MailMessage eMsg = new MailMessage(from, to);
eMsg.Subject = "Your Password Renewal Request";
eMsg.IsBodyHtml = true;
eMsg.Body = "This is the body.";
SmtpClient eClient = new SmtpClient("smtp.gmail.com", 587);
eClient.EnableSsl = true;
eClient.UseDefaultCredentials = true;gmail
// The following email and password used is that of my own gmail email
// that I use for my own personal email.
eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<MyPassword>");
eClient.Send(eMsg);
}
catch (SmtpException ex)
{
throw new ApplicationException("SmtpException has occurred: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}
What else needs to be done?还有什么需要做的?
You cannot use your plain google account password to authenticate to Gmail SMTP server anymore as Google requires two-step authentication now.您不能再使用普通的谷歌帐户密码来验证 Gmail SMTP 服务器,因为谷歌现在需要两步验证。 You'll need to use the App Password instead:
您需要改用应用密码:
You'll get the string with the 4 groups of characters.您将获得包含 4 组字符的字符串。 Now you need to use it in your code:
现在您需要在代码中使用它:
eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<App Password>");
After the removal of less secure apps you can no longer use your actual google password to connect to the smpt server you need to create an apps password.删除安全性较低的应用程序后,您将无法再使用实际的谷歌密码连接到您需要创建应用程序密码的 smpt 服务器。
How to create a Apps Password for connecting to Google's SMTP server.如何创建用于连接到 Google 的 SMTP 服务器的 Apps 密码。
using System;
using System.Net;
using System.Net.Mail;
namespace GmailSmtpConsoleApp
{
class Program
{
private const string To = "[redacted]@gmail.com";
private const string From = "[redacted]@gmail.com";
private const string GoogleAppPassword = "";
private const string Subject = "Test email";
private const string Body = "<h1>Hello</h1>";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential(From , GoogleAppPassword),
EnableSsl = true,
};
var mailMessage = new MailMessage
{
From = new MailAddress(From),
Subject = Subject,
Body = Body,
IsBodyHtml = true,
};
mailMessage.To.Add(To);
smtpClient.Send(mailMessage);
}
}
}
Note: if you are trying to connect users then you could also use Xoauth2 but using an apps password is far easer solution.注意:如果您尝试连接用户,那么您也可以使用 Xoauth2,但使用应用程序密码是更简单的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.