我正在我的Visual Studio中的asp.net mvc web應用程序工作。沒有我的Web應用程序基本上是公共用戶的網站(所以我沒有使用https)。現在我的網站提供了一個正常的與我們聯繫表單,和下面的郵政聯繫的操作方法: -無法發送電子郵件使用谷歌Smtp客戶端
[HttpPost]
public ActionResult Contact(Contact c)
{
var mail = new MailMessage();
// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("****@gmail.com");
mail.To.Add(new MailAddress("****@yahoo.com"));
// Define the message
mail.Subject = "New Feedback from Web Site";
mail.IsBodyHtml = false;
//System.Environment.NewLine
mail.Body = "Dear Sirs," + System.Environment.NewLine +
"New Feedback has been submitted fromWeb site, with the following details :- " + System.Environment.NewLine +
"Name :- " + c.Name + System.Environment.NewLine +
"Email :- " + c.Email +
"Telephone :- " + c.Telephone + System.Environment.NewLine
+
"Message :- " + c.Message
;
// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";
mailclient.Port = 587;
// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;
// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential(
"***@gmail.com",
"***");
mailclient.Send(mail);
TempData["message"] = string.Format("{Your Feedback has been submitted. Thanks");
return RedirectToAction("Contact");
}
,但這將引發以下異常: -
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
,所以我有以下問題: -
什麼導致錯誤?
我應該在我的聯繫人頁面中使用https以便能夠從我的應用程序發送電子郵件?
如何將端口號更改爲465(SSL)而不是587(TLS)? –
@HwasooLee好吧我把它改爲465現在我得到這個錯誤「無法從傳輸連接讀取數據:net_io_connectionclosed。 描述:在執行當前Web請求期間發生未處理的異常請檢查堆棧跟蹤有關錯誤的更多信息以及源代碼的來源 異常詳細信息:System.IO.IOException:無法從傳輸連接讀取數據:net_io_connectionclosed。「 –
@HwasooLee當我使用465我得到了超時例外。我使用下面的代碼「SmtpClient mailclient = new SmtpClient(」smtp.gmail.com「,465); mailclient.Credentials = new NetworkCredential(」*** @ gmail.com「,」****「); mailclient.EnableSsl = true;「但我得到以下例外: - System.dll中發生類型'System.Net.Mail.SmtpException'的異常,但未在用戶代碼中處理 附加信息:操作已超時。 –