我想在ASP.NET MVC中发送电子邮件,这里是我的代码:
public ActionResult Index(MyMailModel objMailModel)
{
if (ModelState.IsValid)
{
string from = "******@gmail.com";
using (MailMessage mail = new MailMessage(from, objMailModel.To))
{
mail.Subject = "Backup Database";
mail.Body = "hahaha";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential network = new NetworkCredential(from, "*******");
smtp.UseDefaultCredentials = true;
smtp.Credentials = network;
smtp.Port = 587;
smtp.Send(mail);
ViewBag.Message = "Sent";
return View("Index", objMailModel);
}
}
else
{
return View();
}
}
但是,我有一个错误:
连接尝试失败,因为连接方在一段时间后未正确响应,或已建立的连接失败,因为已连接的主机无法响应****** :587 ** < / p>
请帮帮我。
感谢。
答案 0 :(得分:1)
检查防火墙或防病毒或端口是否阻止来自服务器的响应