SMTP 异常需要在本地服务器中显示错误消息时使用的 try-catch 块

本文关键字:消息 错误 try-catch 显示 异常 服务器 SMTP | 更新日期: 2023-09-27 18:36:18

每当使用端口时出现异常时都会使用smtp catch块,但它会抛出异常,这很好,it will take a very long time并且异常消息将直接显示在代码本身中,要求是在localserver中执行时显示错误消息,不应该出现在代码中。每当server关闭或invalid email Id或更改smtp port时,应该throw exception,结论是错误消息不应该直接进入代码。它应该显示一条错误消息并停止不应hang application

 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);
    }

这也引发了另一个问题,即每当它进入循环for
执行 send(tomail)函数即使它与这个ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email sent on " + test + "')</script>");行一起无效,也会在每个实例中执行。我只需要在valid EmailID和适当的smtp port可用时才执行此操作。

SMTP 异常需要在本地服务器中显示错误消息时使用的 try-catch 块

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

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);
    }

它需要的时间是它需要"理解"某些事情不正常的时间(自然,这可能是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
    }

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

  • 约翰爸爸的祝酒词
  • 引导程序3 警报

等等...

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