当发送帐户使用两因素身份验证时,如何使用 Gmail 和 SmtpClient 发送电子邮件?

2023-12-30

            SmtpClient smtpClient = new SmtpClient();
            NetworkCredential basicCredential =
                new NetworkCredential("[email protected] /cdn-cgi/l/email-protection", "password");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("[email protected] /cdn-cgi/l/email-protection");

            smtpClient.EnableSsl = true;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;

            message.From = fromAddress;
            message.Subject = "your subject";
            //Set IsBodyHtml to true means you can send HTML email.
            message.IsBodyHtml = true;
            message.Body = "<h1>Hello, this is a demo ... ..</h1>";
            message.To.Add("[email protected] /cdn-cgi/l/email-protection");

            try
            {
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                //Error, could not send the message
                ex.ToString();
            }

// 问题是,这段代码对于没有电话号码保护的电子邮件来说效果很好。将此代码与通过客户电话号码保护(验证)的电子邮件一起使用时会出现异常。

解决方案之一是使用远程服务器访问客户端邮件。

现在我的问题是还有另一种方法来解决这个问题吗?除第三方外。


如果我理解正确的话,你是说 Google 帐户正在使用双因素身份验证。

如果是这种情况,您需要为此创建一个应用程序密码。去https://security.google.com/settings/security/apppasswords https://security.google.com/settings/security/apppasswords以您想要进行双因素身份验证的帐户登录后。

在列表中的下Select App选择“其他”并为其指定名称。单击“生成”,然后写下此密码,因为您只会看到它一次。您将在身份验证中使用它。长度为 16 个字符,空格并不重要,您可以包含或省略它们。我把它们放在这里只是因为。

NetworkCredential basicCredential =
            new NetworkCredential("[email protected] /cdn-cgi/l/email-protection", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当发送帐户使用两因素身份验证时,如何使用 Gmail 和 SmtpClient 发送电子邮件? 的相关文章

随机推荐