2017-09-12 13 views
-1

私はforループに入り、別の機能を使って電子メールを送ります。キャッチして続けてください

エラーが発生した場合、問題が発生したメールの後にメールを送信し続けることはありません。

私はこのエラーを検出し、システムが残りの電子メールを送信し続けたいと思います。以下のコードをご覧ください。

あなたはforeach文の内側 try catchブロックを移動する必要が
public static SendEmailResult setEmailAndSend(DataRow[] rows) 
{ 
    try 
    { 
     SendEmailResult results = new SendEmailResult(); 

     foreach (DataRow row in rows) 
     { 
      try 
      { 
       //Set Values here 
       string strFN = row["FirstName"].ToString(); 
       string strLN = row["LastName"].ToString(); 
       string strEmail = row["Email"].ToString(); 
       //some more here 


       Sent = EmailFunctions.SendEmail(strEmail, strFromAddress, strFromName, strSubject, strFN + " " + strLN, strBody, strSignOffEmbeddedImagePath, strSignOffEmbeddedImageName, strCCReminderEmail); 
      } 
      catch (Exception exc) 
      { 
       //do stuff 

       continue; 
      } 
     } 
    } 
    catch (Exception exc) 
    { 
     //do something 
    } 
} 


public static bool SendEmail(String strToAddress, String strFromAddress, String strFromName, String strSubject, String strRecipientName, String strBody, String strEmbeddedImagePath, String strEmbeddedImageName, String strCCReminderEmail) 
{ 
    try 
    { 
     //SMTPClient settings in webconfig 
     SmtpClient client = new SmtpClient(); 
     client.EnableSsl = true; 

     using (MailMessage message = new MailMessage(
      new MailAddress(strFromAddress, strFromName), 
      new MailAddress(strToAddress, strRecipientName))) 
     { 
      message.IsBodyHtml = true; 
      message.Subject = strSubject; 
      message.Body = strBody; 

      if (!string.IsNullOrEmpty(strCCReminderEmail)) 
       message.CC.Add(strCCReminderEmail); 

      client.Send(message); 

      return true; 
     } 
    } 
    catch (Exception ex) 
    { 
     throw; 

    } 
} 
+0

なぜ私は投票されていますか? –

答えて

1

foreach (DataRow row in rows) 
    { 
        try 
        { 
         //Set Values here 
         string strFN = row["FirstName"].ToString(); 
         string strLN = row["LastName"].ToString(); 
         string strEmail = row["Email"].ToString(); 
         //some more here 


         Sent = EmailFunctions.SendEmail(strEmail, strFromAddress, strFromName, strSubject, strFN + " " + strLN, strBody, strSignOffEmbeddedImagePath, strSignOffEmbeddedImageName, strCCReminderEmail); 
        } 
        catch (Exception exc) 
        { 
         //Log exception!! 
         continue; 
        } 
       } 
+0

申し訳ありません、私はそこに追加するのを忘れました、私は私の例にそれを追加しましたが、まだ問題を抱えていました。 –

+0

問題の内容は何ですか?詳細はこちらからお知らせください。 –

0

あなたはtry catchだけあなたが期待するそれらの領域は、インターネットのようにしても通常の使用で間違って行くIO、ストリームとそうかもしれません必要がありますに。あなたのケースでは

SendEmail(...)throwはここでは必要いないようですので、それは、そう

try 
{ 
    Sent = EmailFunctions.SendEmail(...); 
} 
catch (Exception ex) 
{ 
    // Capture mail send exception 
} 

それとも

public static bool SendEmail(...) 
{ 
    try 
    { 
     ... 
     using (MailMessage message = new MailMessage(...)) 
     { 
      ... 

      return true; 
     } 
    } 
    catch (Exception ex) 
    {   
     // Some function to record which mail didn't send 
     return false; 
    } 
} 

です。

DBアクセスの問題もキャプチャしたい場合は、を外に保つことができます(ただし、continueはループ外では意味がありません)。

+0

私の例を更新しましたが、まだ問題があります –

+0

問題は何ですか?すべての例外メッセージ? –

関連する問題