C# 当smtp异常需要在本地服务器中显示错误消息时,请尝试使用catch块

C# 当smtp异常需要在本地服务器中显示错误消息时,请尝试使用catch块,c#,asp.net,email,smtp,try-catch,C#,Asp.net,Email,Smtp,Try Catch,当使用smtp端口时出现异常时使用Catch块,但它会抛出异常,这很好,它将花费很长时间,并且异常消息将直接显示在代码本身中,要求是在localserver中执行时显示错误消息,并且不应出现在代码中。每当服务器关闭或无效电子邮件Id或smtp端口发生更改时,它应引发异常,结论是错误消息不应直接出现在代码中。它应显示错误消息并停止不应挂起的应用程序 protected void Button2_Click(object sender, EventArgs e) { //string v

当使用
smtp
端口时出现异常时使用Catch块,但它会抛出异常,这很好,
它将花费很长时间
,并且异常消息将直接显示在代码本身中,要求是在
localserver
中执行时显示错误消息,并且不应出现在代码中。每当
服务器
关闭或
无效电子邮件Id
smtp端口
发生更改时,它应
引发异常
,结论是错误消息不应直接出现在代码中。它应显示错误消息并停止不应
挂起的
应用程序

 protected void Button2_Click(object sender, EventArgs e)
{

    //string vv;
    //vv = (string)Session["FID"];
    DateTime sdt = DateTime.Today;

    SqlCommand cmd4 = new SqlCommand();

    int flag=0;


   String test = DateTime.Now.ToString("dd.MM.yyy");
   for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
   {

       string toemail = GridView1.Rows[i].Cells[2].Text;
       string FID1 = GridView1.Rows[i].Cells[0].Text;
       GridViewRow row = GridView1.Rows[i];
       CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
       if (Ckbox.Checked == true)
       {

           sendMail(toemail);
           flag = 1;
           //ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email send Succesfully')</script>");
       }

       if (flag == 1)
       {
           ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email sent  on " + test + "')</script>");

           cn.Open();
           //cmd4.CommandText = "Insert into TrackingFaculty_det  (EmailsentDate)  values (@EmailsentDate) WHERE FID=@FID";
           cmd4.CommandText = "update TrackingFaculty_det SET EmailsentDate=@Email WHERE FID=@FID  ";
           cmd4.CommandType = CommandType.Text;
           cmd4.Connection = cn;

           cmd4.Parameters.Clear();


           cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
           cmd4.Parameters["@Email"].Value = sdt;
           cmd4.Parameters.Add("@FID", SqlDbType.VarChar, 10);
           cmd4.Parameters["@FID"].Value = FID1;
           cmd4.ExecuteNonQuery();
           cn.Close();
           log.Debug("Info : Insert the Email Sent Date of the faculty");
       }
       else
       {
           ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email cannot be sent')</script>");
       }




   }
   log.Debug("Info : Function used to send mail");
}

public void sendMail(String toemail)
{


    try
    {


        MailMessage mail = new MailMessage();
        mail.To.Add(toemail);
        mail.From = new MailAddress("manipal.mcis1@gmail.com");
        mail.Subject = "Remember Mail";
        // string Body = "Please update profile";
        //mail.Body = Body;
        mail.Body = "  Dear Sir/Madam \n\n\n Please update your profile. . \n\n\n Thanks & Regards \n\n\n MCIS,Manipal.";
        //mail.Body = "<html><body> <h2" + "align=center>Dear Sir/Madam" + "</h2> Please update ur profile</body></html>";
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 584;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("manipal.mcis1@gmail.com", "manipal15");
        smtp.EnableSsl = true;
        smtp.Send(mail);


    }
catch (SmtpException ex)
    {

   string msg = "Mail cannot be sent:";
        msg += ex.Message;
        log.Debug("Error: Inside catch block of Mail sending");
        log.Error("Error msg:" + ex);
        log.Error("Stack trace:" + ex.StackTrace);


        throw new Exception(msg);

    }
protectedvoid按钮2\u单击(对象发送者,事件参数e)
{
//串vv;
//vv=(字符串)会话[“FID”];
DateTime sdt=DateTime.Today;
SqlCommand cmd4=新的SqlCommand();
int标志=0;
字符串测试=DateTime.Now.ToString(“dd.MM.yyy”);

对于(int i=0;i它需要的时间是它需要“理解”某些事情不正常的时间(当然,smtp失败可能有不同的原因)

在您的具体代码中,我看到您重新提出了一个异常,这是根据您的需求而不是您想要的,因此可能更好的解决方案是:

    catch (SmtpException ex)
    {

       string msg = "Mail cannot be sent:";
         msg += ex.Message;
         log.Debug("Error: Inside catch block of Mail sending");
         log.Error("Error msg:" + ex);
         log.Error("Stack trace:" + ex.StackTrace);


         SendAsyncMessage(msg);//just an example

    }
对于客户端通知,您可以使用任何您想要的。有不同的库:

等等


如果这不是您想要的,请澄清。

试试这些代码行,它会起作用的

protected void Button2_Click(object sender, EventArgs e)
{

    //string vv;
    //vv = (string)Session["FID"];
    DateTime sdt = DateTime.Today;

    SqlCommand cmd4 = new SqlCommand();

    int flag=0;


   String test = DateTime.Now.ToString("dd.MM.yyy");
   for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
   {

       string toemail = GridView1.Rows[i].Cells[2].Text;
       string FID1 = GridView1.Rows[i].Cells[0].Text;
       GridViewRow row = GridView1.Rows[i];
       CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
       if (Ckbox.Checked == true)
       {

           sendMail(toemail);
           flag = 1;
           //ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email send Succesfully')</script>");
       }

       if (flag == 1)
       {
           //ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email sent  on " + test + "')</script>");

           cn.Open();
           //cmd4.CommandText = "Insert into TrackingFaculty_det  (EmailsentDate)  values (@EmailsentDate) WHERE FID=@FID";
           cmd4.CommandText = "update TrackingFaculty_det SET EmailsentDate=@Email WHERE FID=@FID  ";
           cmd4.CommandType = CommandType.Text;
           cmd4.Connection = cn;

           cmd4.Parameters.Clear();


           cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
           cmd4.Parameters["@Email"].Value = sdt;
           cmd4.Parameters.Add("@FID", SqlDbType.VarChar, 10);
           cmd4.Parameters["@FID"].Value = FID1;
           cmd4.ExecuteNonQuery();
           cn.Close();
           log.Debug("Info : Insert the Email Sent Date of the faculty");
       }




   }
   log.Debug("Info : Function used to send mail");
}

public void sendMail(String toemail)
{


    try
    {


        MailMessage mail = new MailMessage();
        mail.To.Add(toemail);
        mail.From = new MailAddress("manipal.mcis1@gmail.com");
        mail.Subject = "Remember Mail";
        // string Body = "Please update profile";
        //mail.Body = Body;
        mail.Body = "  Dear Sir/Madam \n\n\n Please update your profile. . \n\n\n Thanks & Regards \n\n\n MCIS,Manipal.";
        //mail.Body = "<html><body> <h2" + "align=center>Dear Sir/Madam" + "</h2> Please update ur profile</body></html>";
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("manipal.mcis1@gmail.com", "manipal15");
        smtp.EnableSsl = true;
        smtp.Send(mail);


    }
    catch (SmtpException ex)
    {


        string msg = "Mail cannot be sent because of the server problem:";
        msg += ex.Message;
        log.Debug("Error: Inside catch block of Mail sending");
        log.Error("Error msg:" + ex);
        log.Error("Stack trace:" + ex.StackTrace);

        Response.Write(msg);
        //throw new Exception(msg);

    }
protectedvoid按钮2\u单击(对象发送者,事件参数e)
{
//串vv;
//vv=(字符串)会话[“FID”];
DateTime sdt=DateTime.Today;
SqlCommand cmd4=新的SqlCommand();
int标志=0;
字符串测试=DateTime.Now.ToString(“dd.MM.yyy”);

对于(int i=0;i)这看起来不错,但如果我在catch中使用
MessageBox
,我想它会显示
名称MessageBox
在当前内容中不存在`@SatwikMn:考虑到您的情况下SMTP执行是异步的,您必须在客户端等待响应,如果出现异常,只返回mesage(确定,失败),客户端向用户显示一条消息。因此,处理异常大约需要25-30秒。您不认为处理异常需要很长时间吗?@SatwikMn:如果SMTP花费太多时间来确定失败,可能是系统管理员问题,而不是代码问题。但如果用户在某种方式。如果我使用
Response.Writeline(msg);
而不是
SendAsyncMessage(msg);