[英]Cant send email using Gmail SMTP
我有一個帶有聯系表的網絡 - 我希望將反饋發送到我的 Gmail 帳戶。 為此,我創建了另一個虛擬 gmail 帳戶來發送這些郵件。 代碼看起來像這樣
MailMessage msg = new MailMessage();
msg.To.Add("to_mail@gmail.com");
msg.From = new MailAddress("from_mail@gmail.com");
msg.Subject = "Feedback";
msg.Body = txtName.Text + " " + txtEmail.Text + " " + txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("from_mail@gmail.com", "password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);
當我從 VS 項目的 localhost 運行它時,此代碼工作正常,但是一旦我編譯它並上傳到網絡,它就會停止工作,並出現此錯誤。
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
有沒有人知道是什么導致了它以及如何解決它? 我已經在我的 gmail 帳戶中啟用了對不太安全的應用程序的訪問。 也嘗試過 465 端口,但這也不起作用。
非常感謝,約瑟夫
終於找到了答案。 必須在登錄我的“發件人”Gmail 帳戶的情況下單擊此鏈接。
可能的原因:
在gmail中,如果當前登錄區域和之前登錄區域不同,那么gmail會在登錄前詢問我們一些安全問題。 因此,最好嘗試在您使用該 C# 應用程序的系統中通過瀏覽器登錄一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.