我正在我的visual studio中使用asp.net mvc web应用程序。我的网络应用程序基本上不是公共用户的网站(所以我不使用https)。现在我的网站提供了一个正常的联系我们表格,以及以下邮政联系人行动方法: -
[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,以便能够从我的应用程序发送电子邮件吗?
答案 0 :(得分:1)
尝试添加凭据属性
SmtpClient mailclient = new SmtpClient("smtp.gmail.com", 587);
mailclient.Credentials = new NetworkCredential("username", "pass");
答案 1 :(得分:0)
您可以尝试将您的Gmail帐户设置为允许安全性较低的应用(启用基本身份验证)。