網頁

Error: The SMTP server requires a secure connection or the client was not authenticated.

繼上次Sending Email這篇文章後,開始把網頁發佈上Azure,卻發現出錯了!
錯誤訊息為:

Error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.



因為伺服器位置的關係,Google判定Azure Website連安全性較低的應用程式都不算!

所以Google就提供了「應用程式專用密碼」來解決這個安全性問題,步驟如下:


2.點選兩步驟驗證→開始設定


3.通過手機認證


4.認證完後,回到一開始Google帳號設定頁面,選擇應用程式密碼→設定


5.選擇你的應用程式,或自訂名稱

6.按產生密碼

7.複製密碼!


8.使用剛剛產生的密碼寄送EMAIL


async public Task< string> SendEmailAsync(  )
       {           
            string emailFrom = "xxx@gmail.com"; //"email@yahoo.com";
            string password = "剛剛複製的應用程式密碼";
            string emailTo = //"someone@domain.com";
            string subject = "Wecare診所檢驗提醒" ;//"Hello";
            string body = "內容";  "Hello, I'm just writing this to say Hi!";

            using ( MailMessage mail = new MailMessage())
            {
                try
                {
                    mail.From = new MailAddress(emailFrom);
                    mail.To.Add(emailTo);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = false;
                    // Can set to false, if you are sending pure text.
                    //mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
                    //mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
                    using ( SmtpClient smtp = new SmtpClient())
                    {
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(emailFrom, password);
                        smtp.EnableSsl = true;
                        await smtp.SendMailAsync(mail);
                    }
                }
               
               catch(Exception e) { MessageBox.Show(e.ToString()); }

                return "檢驗提醒EMail已發送!" ;
            }          
        }   


最後發布到雲端就大功告成囉!

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...