無法從 Azure 雲服務使用 Gmail smtp
我通過 Gmail 的 smtp 發送電子郵件的程式碼:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("my_user_name", "my_password"); MailMessage message = new MailMessage(new MailAddress("from...@gmail.com"), new MailAddress("to...@gmail.com")); message.Body = "body"; message.Subject = "subject"; client.Send(message);該程式碼適用於我的本地電腦,並且當我在 Azure 上作為“網站”發佈時。
但是當我在**“雲服務”**中發佈時,我得到了這個例外:
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. Learn more atWindows Azure“網站”與“雲服務”有什麼不同會產生這種效果嗎?
謝謝!
在 Web.config 中使用以下 SMTP 設置:
<system.net> <mailSettings> <smtp deliveryMethod="Network"> <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" userName="xxxxxxx@gmail.com" password="xxxxxxxxxxx"/> </smtp> </mailSettings> </system.net>我認為您傳遞了錯誤的憑據。在您的使用者名中使用 @gmail.com 後綴並嘗試將 bodyhtml 屬性設置為 true ……
希望這對你有用..它對我來說總是正確的..
在這個 SO 執行緒中檢查答案的評論。
我遇到了這個確切的問題。但是,無論我使用的是
<system.net>配置設置還是使用了正確的憑據、主機、埠等,我都遇到了這個問題。問題在於 Google 拒絕了來自 Azure 的身份驗證請求。我通過登錄我在程式碼中用於 SMTP 客戶端的 Gmail 帳戶發現了這一點。登錄 Gmail 帳戶後,我注意到一個 red-bar-header-warning 說
有人從您的帳戶不常見的位置登錄。如果不是您,請立即更改您的密碼。
除了警告之外,我還收到一封電子郵件,上面寫著:
最近有人嘗試使用應用程序登錄您的 Google 帳戶 xxxxx@gmail.com。我們阻止了登錄嘗試,以防這是試圖訪問您帳戶的劫持者。請查看登錄嘗試的詳細資訊:
- 格林威治標準時間 2012 年 8 月 27 日星期一晚上 10:33:59
- IP地址:168.62.48.183
- 地點:美國
如果您無法辨識此登錄嘗試,則其他人可能正在嘗試訪問您的帳戶。您應該立即登錄您的帳戶並重置密碼。了解如何在 <http://support.google.com/accounts?p=reset_pw>
如果是您本人,並且您想授予此應用程序訪問您帳戶的權限,請完成 http://support.google.com/mail?p=client_login中列出的故障排除步驟
此致 Google 客戶小組敬上
按照提供的連結中列出的步驟進行操作後,我的 Azure 網站能夠成功登錄到我的 Gmail 帳戶並將 Gmail 用作 SMTP 客戶端。