Asp.net mvc 4 MVC使用SmtpClient发送电子邮件
这是我的代码尝试发送邮件如果你有任何想法来解决这个问题,请帮我Asp.net mvc 4 MVC使用SmtpClient发送电子邮件,asp.net-mvc-4,Asp.net Mvc 4,这是我的代码尝试发送邮件如果你有任何想法来解决这个问题,请帮我 enter code here public ActionResult ContactForm(Contact cnt) { BlogApplicationEntities db = new BlogApplicationEntities(); if (ModelState.IsValid) { try {
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
执行以下行时:
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
smtp.Sendmsg
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
它抛出一个错误:
'无法解析远程名称:https://smtp.gmail.com"
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
我能做些什么来解决这个问题,有人能帮我吗?换成smtp.gmail.com,你需要验证一个正确的gmail帐户。以下示例显示。您需要将SMTP主机更改为SMTP.gmail.com:
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
主机名不包括传输协议,但当您指定EnableSsl=true时,您将传输协议设置为SSL。请注意,如果您是从gmail发送,请确保您的网络凭据使用的是gmail帐户。我将smtp更改为smtp.Host=smtp.gmail.com,但现在它抛出了一个错误:smtp服务器需要安全连接或客户端未经身份验证。服务器响应为:需要5.5.1身份验证
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}
我想我的问题是空凭证 我真诚地希望这不是您的hotmail密码中的凭据。
enter code here
public ActionResult ContactForm(Contact cnt)
{
BlogApplicationEntities db = new BlogApplicationEntities();
if (ModelState.IsValid)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.To.Add(new MailAddress("send to mail address"));
msg.Subject = "Contact Us";
msg.Body += "\nFirst Name=" + cnt.FirstName;
msg.Body += "Last Name=" + cnt.LastName;
msg.Body += "Email=" + cnt.Email;
msg.From = new MailAddress("mailaddress", "Jhon");
msg.Body += "Comments=" + cnt.Comment;
msg.IsBodyHtml = true;
smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
smtp.Host = "https://smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Port = 587;
smtp.Send(msg);
msg.Dispose();
db.Contacts.Add(cnt);
db.SaveChanges();
return View("Success");
}
catch (Exception)
{
return View("Error");
}
}
return View();
}