C#通过Gmail账户发送邮件

C# sending email via Gmail account

我正在使用简单的 CMD "Service" 扩展我的 Web 应用程序,它应该向新注册的用户发送验证电子邮件。我的问题是通过验证 Gmail 帐户,抛出以下异常:

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

我已经尝试在我自己的 IMAP 服务器上进行身份验证,但也没有成功。 后来我尝试使用XAMP Mercury邮件服务器,这不是最好的解决方案,由于完全依赖于本地配置,所以我放弃了 那个想法。 以后,我只想为应用程序创建一个新的 google 帐户,因此不需要维护。

  String body = "<head>" +
            "Here comes some logo" +
          "</head>" +
          "<body>" +
            "<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
            "<a>Dear User, </a>" + Environment.NewLine +
            "<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
            "<a>This is the last step towards using our app.</a>" + Environment.NewLine +
            "<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
            "<a>[Callback url]</a>" +
          "</body>";
  try
  {
     SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
     smtpClient.UseDefaultCredentials = false;
     smtpClient.Credentials = new NetworkCredential()
     {
        UserName = "myemail@gmail.com",
        Password = "mypassword"
     };
     smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
     smtpClient.EnableSsl = true;
     smtpClient.Send("targetemail@targetdomain.xyz", "myemail@gmail.com", "Account verification", body);
  }
  catch (Exception ex)
  {
  }

我只想能够通过 Gmail 服务器发送电子邮件,没有任何例外。 我是否需要任何 NuGet 包,使用不同的方法?

如果您的 Gmail 帐户启用了两步验证,您将必须创建一个 App-Specific Password 来进行身份验证。

另请注意,SmtpClient 是 IDisposable - 您应该将其放在 using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) { ... } 块中,以便 SMTP 连接 RSET、QUIT 和正确关闭。

==编辑==

此外,您似乎在 smtpClient.Send 上切换了 fromrecipients 参数。

string body = "<head>" +
            "Here comes some logo" +
        "</head>" +
        "<body>" +
            "<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
            "<a>Dear User, </a>" + Environment.NewLine +
            "<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
            "<a>This is the last step towards using our app.</a>" + Environment.NewLine +
            "<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
            "<a>[Callback url]</a>" +
        "</body>";
try
{
    using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
    {
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new NetworkCredential()
        {
            UserName = Config.Username,
            Password = Config.Password,
        };
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;

        //Oops: from/recipients switched around here...
        //smtpClient.Send("targetemail@targetdomain.xyz", "myemail@gmail.com", "Account verification", body);
        smtpClient.Send("myemail@gmail.com", "targetemail@targetdomain.xyz", "Account verification", body);
    }
}
catch (Exception e)
{
    Console.Error.WriteLine("{0}: {1}", e.ToString(), e.Message);
}