当我删除评论“//stmp.timeout”时,它会给出超时错误。我该怎么做才能解决这个问题?
这是我的代码:
public ActionResult Index(EmailModel model)
{
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 180;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("sheikh.abm@gmail.com ","somepassword");
//smtp.Timeout = 20000;
}
try
{
smtp.Send("sheikh.abm@gmail.com",model.To, model.Subject, model.Message);
return View("Index");
}
catch (Exception ex)
{
Console.WriteLine(ex); //Should print stacktrace + details of inner exception
if (ex.InnerException != null)
{
Console.WriteLine("InnerException is: {0}", ex.InnerException);
}
}
return View("Index");
答案 0 :(得分:1)
在这一行
smtp.Credentials = new NetworkCredential("sheikh.abm@gmail.com ","somepassword
gmail.com
之后有一个空格。这可能会阻止登录Gmail。删除它。
另外,我认为用于向Gmail发送邮件的端口是465用于SSL或587用于TLS / STARTTLS。
smtp.Port = 465;