当发送帐户使用两因素身份验证时,如何通过Gmail和SmtpClient发送电子邮件? [英] How do I send an email with Gmail and SmtpClient when the sending account uses two factor authentication?

查看:157
本文介绍了当发送帐户使用两因素身份验证时,如何通过Gmail和SmtpClient发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

            SmtpClient smtpClient = new SmtpClient();
            NetworkCredential basicCredential =
                new NetworkCredential("sender@gmail.com", "password");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("sender@gmail.com");

            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("receiver@gmail.com");

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

//问题是,此代码可在没有电话号码保护的情况下对gmail正常工作.将此代码与通过客户电话号码受保护(验证)的Gmail结合使用时,会发生异常.

// The thing is that this code works fine for gmails without phone number protection. Exception occurs when using this code with gmails that are protected(verified) via the client phone number.

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

One of the solution is to use a remote server to access clients mails.

现在我的问题是还有另一种方法可以解决此问题吗?除了第三方.

Now my question is there another method to solve this issue ? other than third parties.

推荐答案

如果我正确理解您的意思,就是说Google帐户使用的是双重身份验证.

If I understand you correctly, you're saying the Google account is using two-factor authentication.

在这种情况下,您需要为此创建一个应用程序密码.登录后,请转到 https://security.google.com/settings/security/apppasswords 作为您要进行双重身份验证的帐户.

If that's the case, you need to create an Application Password for this. Go to https://security.google.com/settings/security/apppasswords once logged in as the account you want to two-factor auth with.

在列表的 Select App 下,选择"Other"并为其命名.单击生成",然后将此密码写为"DOWN",因为您只会一次看到该密码.您将在身份验证中使用它.长度为16个字符,空格无关紧要,您可以包含它们,也可以忽略它们.我之所以把它们包括在这里是因为.

In the list, under Select App choose "Other" and give it some name. Click Generate, and write this password DOWN cause you will only ever see it ONCE. You will use this in your authentication. It will be 16-characters long and the spaces don't matter, you can include them or omit them. I included them here just because.

NetworkCredential basicCredential =
            new NetworkCredential("sender@gmail.com", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();

这篇关于当发送帐户使用两因素身份验证时,如何通过Gmail和SmtpClient发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆