![](/img/trans.png)
[英]SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
[英]The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.?8
我正在使用c#創建示例電子郵件發件人,但顯示錯誤為:
SMTP服務器需要安全連接,或者客戶端未通過身份驗證。 服務器響應為:5.5.1需要身份驗證。
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("toAddress@gmail.com");
mail.From = new MailAddress("fromAddress@gmail.com", "Test Mail !", System.Text.Encoding.UTF8);
mail.Subject = "Test Mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "Test message";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("myMail@gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(mail);
網頁配置:
<system.net>
<mailSettings>
<smtp from="myMail@gmail.com">
<network host="smtp.gmail.com" password="password"
port="587" userName="myMail@gmail.com" />
</smtp>
</mailSettings>
</system.net>
如果您啟用了雙重身份驗證,則需要為自己的Google帳戶生成一個應用密碼。
生成該密碼的步驟如下( [Google頁面] | [存檔頁面] ):
- 訪問您的應用密碼頁面。 系統可能會要求您登錄您的Google帳戶。
- 點擊底部的“選擇應用”,然后選擇您正在使用的應用。
- 點擊選擇設備,然后選擇您正在使用的設備。
- 單擊生成。
- 按照說明在設備上輸入應用密碼(黃色欄中的16個字符代碼)。
- 單擊完成。
點擊完成后,您將不會再看到該應用的密碼。 但是,您將看到已為其創建應用程序密碼的應用程序和設備的列表。
然后,在您的NetworkCredentials中,將密碼替換為生成的16位密碼。
嘗試通過代碼發送郵件,當您收到錯誤消息時,請在2或3分鍾后登錄到您的Gmail帳戶並檢查收件箱。 您會收到這樣的郵件:
#;
#;
#;
#;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.