2013-10-15 33 views
2
protected void Button2_Click(object sender, EventArgs e) {   
    //string vv; 
    //vv = (string)Session["FID"]; 
    DateTime sdt = DateTime.Today; 
    SqlConnection cn1 = new SqlConnection(); 
    SqlCommand cmd4 = new SqlCommand(); 

    cn1.ConnectionString = @"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True"; 

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

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

      cn1.Open(); 
      //cmd4.CommandText = "Insert into TrackingFaculty_det (EmailsentDate) values (@EmailsentDate) WHERE [email protected]"; 
      cmd4.CommandText = "update TrackingFaculty_det SET [email protected] WHERE [email protected] "; 
      cmd4.CommandType = CommandType.Text; 
      cmd4.Connection = cn1; 

      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(); 
      cn1.Close(); 
     }      
    } 
} 

public void sendMail(String toemail) { 
    try { 
     MailMessage mail = new MailMessage(); 
     mail.To.Add(toemail); 
     mail.From = new MailAddress("[email protected]"); 
     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("[email protected]", "manipal15"); 
     smtp.EnableSsl = true; 
     smtp.Send(mail); 
    } catch (Exception ex) {    
     //System.ArgumentException argx = new System.ArgumentException("There is some problem in sending mail please try again later"); 
     //throw argx; 
     //Console.WriteLine("There is some problem in sending mail please try again later", ex.Message); 
     Response.Write(ex.ToString()); 
    } 

碼的這些線路發送郵件能夠使用mailsmtp端口,它的做工精細送。 通過其他潛在的看點可能有一些exceptions在哪裏可能有輸入Dummy email[email protected]或可能有一些server issues在郵件不能發送在那個特定的時間或可能有一些其他異常,在那些例子異常處理功能必須起作用,並應顯示一個彈出窗口或一些錯誤信息,如郵件無法發送,是否有任何可能性?異常處理在使用SMTP端口

回答

0

您可以簡單地添加一個<asp:Label>上/下GridView和excepetion的情況下,設置其Text錯誤消息:

<asp:Label ID="lblMsg" runat="server"></asp:Label> 

..

try{ 
    sendMail(toemail); 
} 
catch(Exception ex){ 
    lblMsg.Text = ex.Message; // or whatever message you want to show 
    lblMsg.ForeColor = Color.Red // Red shows error 
} 

... 

public void sendMail(String toemail){ 

    try{ 
     ... 
    } 
    catch(Exception ex){ 
     throw ex; // Don't use Response.Write 
    } 
} 
6

SMTP服務器級別的問題應使用SmtpException進行處理。
所以,你可以按照如下

catch (SmtpException ex) 
{  
    Response.Write(ex.ToString()); 
} 

但是修改catch塊,有時不通過SmtpException處理的郵箱的問題。
爲此,您可以使用SmtpFailedRecipientException用於包裝從單個郵箱報告的錯誤。此異常包含enum類型的StatusCode屬性,它將告訴我們錯誤的確切原因。

請參考下面的catch塊

catch (SmtpFailedRecipientException ex)   
{ 
     SmtpStatusCode statusCode = ex.StatusCode;  

     if (statusCode == SmtpStatusCode.MailboxBusy || 
      statusCode == SmtpStatusCode.MailboxUnavailable || 
      statusCode == SmtpStatusCode.TransactionFailed) 
     {     
      // Display message like 'Mail box is busy', 'Mailbox is unavailable' or 'Transaction is failed' 
     } 
     else 
     { 
      throw; 
     } 
} 

您可以使用此

+0

如果郵件被髮送到'Dummy' ID'防爆處理個人的錯誤:ABC @ gmail.com'那麼會是怎樣的解決方案 –

+1

驗證用戶電子郵件的最佳方式是讓用戶驗證它。當用戶收到郵件時,請求他單擊郵件中提供的鏈接以驗證電子郵件ID。如果用戶驗證,郵件ID不是假的.. –