2017-01-23 22 views
0
//i have left out some parameters for readability 
public static string MailForCandidate(string username, string FromAddr, string ToAddr, string strSubject) 
     { 
      try 
      { 
       MailMessage msg = new MailMessage(); 
       string strMailBody = "Test body"; 

       msg.To = ToAddr; 
       msg.From = FromAddr; 

       msg.BodyFormat = MailFormat.Html; 
       msg.Priority = MailPriority.High; 
       msg.Subject = strSubject; 
       msg.Body = strMailBody.ToString(); 
       SmtpMail.SmtpServer = ConfigurationManager.AppSettings["mailServer"].ToString(); 
       try 
       { 
        SmtpMail.Send(msg); 
        msg = null; 
        return ""; 
       } 
       catch (Exception ex) 
       { 
        Util.Common.LogErrorMessage(ex); 
        msg = null; 
        return username; 
       } 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

我有上述函數用於從IIS託管的.NET 3.5 asp.net應用程序發送電子郵件。某些電子郵件在asp.net中使用system.web.mail時延遲或未送達

通過單擊按鈕發送電子郵件,並從數據庫表中讀取收件人。

我遇到的問題是,有些收件人收到他們的電子郵件沒有任何問題,其他人收到他們的電子郵件太長時間(有時在24小時後),在這段時間他們應該參加的事件已經過期(這對我的公司有法律影響)。最後,其他人完全沒有收到電子郵件。

以上MailForCandidate功能正在從SendCandidateMail下面調用。

 private void SendCandidateMail(int intEmailType) 
    { 
     try 
     { 
      ArrayList arrPending = new ArrayList(); 
      ArrayList arrUnsent = new ArrayList(); 
      string strCandidatename = string.Empty; 
      string stractualname = string.Empty; 
      int intUnsentCandCount = 0; 
      if (hdnUnsentNames.Value.Trim() != string.Empty) 
      { 
       arrUnsent.AddRange(hdnUnsentNames.Value.Split(',')); 
      } 
      if (hdnPendingNames.Value.Trim() != string.Empty) 
      { 
       arrPending.AddRange(hdnPendingNames.Value.Split(',')); 
      } 
      hdnUnsentNames.Value = string.Empty; 
      hdnPendingNames.Value = string.Empty; 
      if (!string.IsNullOrEmpty(hdnUnsent.Value) && !string.Empty.Equals(hdnUnsent.Value)) 
      { 
       string[] strUnsIds = hdnUnsent.Value.Split('~'); 
       for (int i = 0; i < strUnsIds.Length; i++) 
       { 
        DataSet dtsetCandidate = CandidatesListBL.GetCandidateDetails(Convert.ToInt32(strUnsIds[i])); 
        stractualname = arrUnsent[i].ToString(); 
        if (dtsetCandidate.Tables[0].Rows.Count > 0) 
        { 
         if (dtsetCandidate.Tables[0].Rows[0]["Time"].ToString() != "0") 
         { 
          //i have left out some parameters for readability 
          strCandidatename = SendMail.MailForCandidate(dtsetCandidate.Tables[0].Rows[0]["User_Id"].ToString(), intEmailType); 
         } 
         else 
          strCandidatename = SendMail.MailForCandidateNoTime(dtsetCandidate.Tables[0].Rows[0]["User_Id"].ToString(), intEmailType); 

         if (strCandidatename.Trim().Equals(string.Empty)) 
         { 
          hdnUnsentNames.Value = hdnUnsentNames.Value + stractualname + ","; 
          intUnsentCandCount = intUnsentCandCount + 1; 

          if (Convert.ToInt32(EnumMaster.EmailType.Customized) != intEmailType) 
          { 
           CandidatesListBL.UpdateCandidateStatus(Convert.ToInt32(strUnsIds[i]), "Sent"); 
           CandidatesListBL.UpdateDateSent(Convert.ToInt32(strUnsIds[i])); 
          } 
         } 
        } 
       } 
       hdnUnsent.Value = string.Empty; 
      } 
     } 
     catch (Exception ex) 
     { 
      WESMessage.DisplayMessage(this, this.UpdatePanel1, DanielHac.TamperProofString.QueryStringEncode("MailFailed"), this.strinfo); 
      Common.LogErrorMessage(ex); 
     } 
    } 

以下是錯誤日誌文本文件中記錄的內容。

01/22/2017 3:23:04 PM ==>異常消息:線程被中止。 01/22/2017 3:23:04 PM ==>異常來源:App_Code 異常目標網站:System.String MailForCandidate(System.String,System.String,System.String,System.Collections.ArrayList,System.String ,System.String,System.String,System.String,System.String,System.String,UserSession,System.String,System.String,Int32,Int32) 異常堆棧跟蹤:在WES.Util.SendMail.MailForCandidate(String strUserID,String username,String password,ArrayList alFiles,String FromName,String FromAddr,String ToAddr,String title,String strSubject,String strCustomMsg,UserSession UserObj,String strDeadLine,String strTime,Int32 intOfficeId,Int32 intMailType) at Pages_ExerciseDetails.SendCandidateMail( Int32 intEmailType) 字符串異常:System.Threading.ThreadAbortException:線程正在中止。在WES.Util.SendMail.MailForCandidate(String strUserID,String username,String password,ArrayList alFiles,String FromName,String FromAddr,String ToAddr,String title,String strSubject,String strCustomMsg,UserSession UserObj,String strDeadLine,String strTime,Int32上的 intOfficeId,的Int32 intMailType) 在Pages_ExerciseDetails.SendCandidateMail(的Int32 intEmailType)

而且

2017年1月22日下午3點23分04秒==>異常消息:線程已被中止。 2017年1月22日下午3點23分04秒==>異常來源:App_Web_kxc2lbj5 異常目標站點:空隙SendCandidateMail(的Int32) 異常堆棧跟蹤:在Pages_ExerciseDetails.SendCandidateMail(的Int32 intEmailType) 在Pages_ExerciseDetails.ibtnSend_Click(對象發件人,EventArgs e) 字符串異常:System.Threading.ThreadAbortException:線程正在中止。 在Pages_ExerciseDetails.SendCandidateMail(的Int32 intEmailType) 在Pages_ExerciseDetails.ibtnSend_Click(對象發件人,EventArgs的)

另外

2017年1月22日下午3點23分04秒==>異常消息: 請求超時。 101/22/2017 3:23:04 PM ==>異常源:異常目標站點: 異常堆棧跟蹤:字符串異常:System.Web。HttpException: 請求超時。

然後

101/22/2017下午3時31分35秒==>異常消息: 類型 'System.Exception的' 引發的異常。 2017年1月22日下午3點31分35秒==>異常 來源:App_Web_kxc2lbj5異常目標站點:空隙 SendCandidateMail(Int32)已異常堆棧跟蹤:在 Pages_ExerciseDetails.SendCandidateMail(的Int32 intEmailType)異常 至字符串:System.Exception的:拋出異常類型'System.Exception'爲 。在Pages_ExerciseDetails.SendCandidateMail(的Int32 intEmailType)

我覺得很難troubleshot因爲它是在生產ONLY發生。在QA和Dev中,使用相同的SMTP服務器的電子郵件正在順利運行。

+0

MailForCandidateNoTime和MailForCandidate有什麼區別? – Karlta05

+0

除了郵件正文區別之外,沒有什麼明顯區別 – StackTrace

+0

只是想知道我是否應該考慮使用System.Net.Mail命名空間,而不是因爲system.web.mail只是失敗了很多信息? – StackTrace

回答

0

我只是在這裏發表我的評論作爲答案。

我解決了這個BU只是丟棄了過時的system.web.mail。 I 使用System.Net.Mail &重新編寫代碼,所有問題都得到解決, 的執行速度甚至更快。