操作在ASP.NET上超时错误

时间:2018-04-01 03:28:15

标签: asp.net c#-4.0 smtp

我通过asp.net SMTP服务器发送电子邮件时出错。它说操作已经超时。如何克服异常?

   try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient();

        mail.From = new MailAddress("mymailid@hotmail.com");
        mail.To.Add("yourmailid@gmail.com");
        mail.Subject = "Verify your account";
        mail.Body = "Your OTP for your account verification is "+OTP.ToString()+". Enter your OTP in verification window.";
        mail.IsBodyHtml = true;

        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Timeout = 20000;
        SmtpServer.Send(mail);
        errmsgShow.Text = "Mail sent";
    }
    catch (Exception e)
    {
        //string errMsg = "alert('OTP sending failed. Error: " +e.ToString()+ "')";
        //ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", errMsg, true);
        //Console.WriteLine(e.Message.ToString());
        errmsgShow.Text = e.ToString();
    }

我还在web.config

中包含了以下内容
<system.net>
<mailSettings>
  <smtp deliveryMethod="Network">
    <network host="smtp.live.com" port="25" userName="mymailid@hotmail.com" password="password" enableSsl="true"/>
  </smtp>
</mailSettings>
</system.net>

1 个答案:

答案 0 :(得分:0)

尝试删除SmtpServer.Timeout = 20000;

更新

我看到你有SmtpClient SmtpServer = new SmtpClient();

SmtpServer是SmtpMail SmtpMail.SmtpServer的属性。尝试更改变量名称,例如

SmtpClient mailClient = new SmtpClient();