[英]Two factor authentication not sending code to email when UserName is not an email address
[英]How do I send an email with Gmail and SmtpClient when the sending account uses two factor authentication?
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一起使用時,會發生異常。
解決方案之一是使用遠程服務器訪問客戶端郵件。
現在我的問題是還有另一種方法可以解決此問題嗎? 除了第三方。
如果我正確理解您的意思,是說Google帳戶正在使用兩因素身份驗證。
在這種情況下,您需要為此創建一個應用程序密碼。 以您要進行雙重身份驗證的帳戶身份登錄后, 請轉到https://security.google.com/settings/security/apppasswords 。
在列表中的“ Select App
”下, Select App
“其他”並為其命名。 單擊“生成”,然后將此密碼記為“ DOWN”,因為您只會一次看到該密碼。 您將在身份驗證中使用它。 長度為16個字符,空格無關緊要,您可以包含或忽略它們。 我之所以將它們包括在這里是因為。
NetworkCredential basicCredential =
new NetworkCredential("sender@gmail.com", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.