【问题标题】:C# sending email via Gmail accountC# 通过 Gmail 帐户发送电子邮件
【发布时间】:2019-08-25 09:26:56
【问题描述】:

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

“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。”

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

  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 包,使用不同的方法?

【问题讨论】:

    标签: c# smtp gmail


    【解决方案1】:

    如果您的 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);
    }
    

    【讨论】:

    • 谢谢,这真的很有帮助。但是我将我的电子邮件作为“HTML 纯文本”收到,其中 HTML 代码没有被格式化。你知道如何强制执行这种格式吗?
    • 使用MailMessage 实例构造消息并设置IsBodyHtml = true
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2010-10-19
    • 2016-05-28
    • 2011-12-15
    • 2013-03-24
    相关资源
    最近更新 更多