2012-03-21 349 views
1

我只需要幫助。基本上,我正在創建一個窗口應用程序,向我們的客戶發送批量電子郵件。 「email」和「attachment」字段來自數據庫。附件字段僅包含文件所在的路徑,代碼正在工作,但不會收到5封電子郵件,我收到15封電子郵件。在數據庫中發送包含附件路徑的批量電子郵件

注:我的數據庫只包含5條記錄,所以我應該僅僅接收5封電子郵件有附件:

你能幫我請,謝謝!

這裏是我的代碼:

 string email; 
     string attachment; 
     ArrayList emailList = new ArrayList(); 
     ArrayList attachList = new ArrayList(); 
     private static readonly Logger log = new _EventLogger(); 

     private void btnSend_Click(object sender, EventArgs e) 
     { 
      conn.Open(); 
      SqlCommand cmdgetEmail = new SqlCommand("Select EMAIL, PATH from MEMBERREQUIREMENTS WHERE STATUS=0", conn); 
      SqlDataReader getEmail = cmdgetEmail.ExecuteReader(); 
      //count = 0; 
      while (getEmail.Read()) 
      { 
       //count++; 
       //email = getEmail.GetValue(i).ToString(); 
       //emailList.Add(email); 
       //i = i + 1 - 1; 
       email = getEmail.GetString(0); 
       emailList.Add(email); 
       attachment = getEmail.GetString(1); 
       attachList.Add(attachment); 
      } 
      getEmail.Close(); 
      conn.Close(); 
      sendMail(); 
     } 
      private void sendMail() 
      { 

       string from="[email protected]"; 
       foreach (string sendTo in emailList) 
       { 

        foreach (string sendAttachments in attachList) 
        { 
        MailMessage mail = new MailMessage(); 
        mail.To.Add(sendTo); 
        mail.From = new MailAddress(from, "Company Name'", Encoding.UTF8); 
        mail.Subject = subject; 
        mail.Body = msgBodyHead + msgBodyHead2 + msgDate + msgGreet + msgBody + msgAdobe + msgAssistance + msgCompliment + msgfooter; 
        mail.IsBodyHtml = true; 
        mail.Priority = MailPriority.High;  
        mail.Attachments.Add(new Attachment(sendAttachments)); 

         SmtpClient client = new SmtpClient(); 
         client.Credentials = new System.Net.NetworkCredential(from, "password"); 
         client.Host = "192.167.89.0"; 
         client.EnableSsl = false; 
         try 
         { 

          progress(); 
          client.Send(mail); 

         } 

         catch (Exception ex) 
         { 
          ProgressBar1.Visible = false; 
          timer1.Enabled = false; 
          Exception excpt = ex; 
          string errorMessage = string.Empty; 

          while (excpt != null) 
          { 

           errorMessage += excpt.ToString(); excpt = excpt.InnerException; 
           log.Error("Email - LMS Application Error", ex); 
           lblError.Text = "There was an error occured while processing your request.\n Please see Event Viewer for more details."; 
           lblError.ForeColor = System.Drawing.Color.Red; 
          } 
         } 

        } 
       } 
      } 
+1

注意'[代碼]'和'[/代碼]'僞標籤毫無意義。格式化代碼的最佳方式是在每行前面加上四個空格。如果您選擇所有代碼,然後點擊編輯器中的「{}」按鈕,它就會正常工作。 – sarnold 2012-03-21 00:19:27

+0

抱歉,wysywig編輯器在我們的防火牆上被阻止,所以我無法看到它。 – Dhenn 2012-03-21 00:20:33

+0

我相信現在好了 – Dhenn 2012-03-21 00:22:26

回答

4

它看起來像你從你的MEMBERREQUIREMENTS表拉了一系列的記錄包含電子郵件地址,並以附件的路徑,並建立電子郵件列表地址和附件路徑。

然後你在迭代這些發送,爲每個地址和附件組合發送一封電子郵件。我猜這不是你想要做的。 我想象一下:

  • 每個收件人應該只收到其記錄中列出的附件。在這種情況下,您不希望遍歷每個地址的附件,或者每個收件人都可能收到每個附件,但附件應列在單獨的表中,而不是存儲在每個地址的記錄中你的MEMBERREQUIREMENTS表。

UPDATE:

下面是一些代碼做前者。請注意,這是您發佈的代碼只是最低限度的修改,並且不固定變量名稱等或提供任何附加的錯誤檢查(你可能想要做):

private static readonly Logger log = new _EventLogger(); 


private void btnSend_Click(object sender, EventArgs e) 
{ 
    conn.Open(); 
    SqlCommand cmdgetEmail = new SqlCommand("Select EMAIL, PATH from MEMBERREQUIREMENTS WHERE STATUS=0", conn); 
    SqlDataReader getEmail = cmdgetEmail.ExecuteReader(); 
    while (getEmail.Read()) 
    { 
     email = getEmail.GetString(0); 
     attachment = getEmail.GetString(1); 
     this.sendMail(email, attachment) 
    } 
    getEmail.Close(); 
    conn.Close(); 
} 

private void sendMail(string sendTo, string sendAttachments) 
{ 
    MailMessage mail = new MailMessage(); 
    mail.To.Add(sendTo); 
    mail.From = new MailAddress(from, "Company Name'", Encoding.UTF8); 
    mail.Subject = subject; 
    mail.Body = msgBodyHead + msgBodyHead2 + msgDate + msgGreet + msgBody + msgAdobe + msgAssistance + msgCompliment + msgfooter; 
    mail.IsBodyHtml = true; 
    mail.Priority = MailPriority.High; 
    mail.Attachments.Add(new Attachment(sendAttachments)); 

    SmtpClient client = new SmtpClient(); 
    client.Credentials = new System.Net.NetworkCredential(from, "password"); 
    client.Host = "192.167.89.0"; 
    client.EnableSsl = false; 
    try 
    { 

     progress(); 
     client.Send(mail); 

    } 

    catch (Exception ex) 
    { 
     ProgressBar1.Visible = false; 
     timer1.Enabled = false; 
     Exception excpt = ex; 
     string errorMessage = string.Empty; 

     while (excpt != null) 
     { 

      errorMessage += excpt.ToString(); excpt = excpt.InnerException; 
      log.Error("Email - LMS Application Error", ex); 
      lblError.Text = "There was an error occured while processing your request.\n Please see Event Viewer for more details."; 
      lblError.ForeColor = System.Drawing.Color.Red; 
     } 
    } 
} 
+0

你明白了,這就是我真正想要的:每個收件人都應該收到 – Dhenn 2012-03-21 00:30:03

+0

你能給我一些代碼來糾正它嗎 – Dhenn 2012-03-21 00:31:31

+0

是的,我已經編輯了我的答案,包括代碼。 – Ergwun 2012-03-21 00:40:50

0

這是因爲你已經有了在你的附件foreach循環中調用client.Send(mail);。它應該在該循環之外。

嘗試改變sendMail方法是:

private void sendMail() 
{ 
    string from = "[email protected]"; 
    foreach (string sendTo in emailList) 
    { 
     MailMessage mail = new MailMessage(); 
     mail.To.Add(sendTo); 
     mail.From = new MailAddress(from, "Company Name'", Encoding.UTF8); 
     mail.Subject = subject; 
     mail.Body = msgBodyHead + msgBodyHead2 + msgDate + msgGreet + msgBody + msgAdobe + msgAssistance + msgCompliment + msgfooter; 
     mail.IsBodyHtml = true; 
     mail.Priority = MailPriority.High; 
     foreach (string sendAttachments in attachList) 
     { 
      mail.Attachments.Add(new Attachment(sendAttachments)); 
     } 

     SmtpClient client = new SmtpClient(); 
     client.Credentials = new System.Net.NetworkCredential(from, "password"); 
     client.Host = "192.167.89.0"; 
     client.EnableSsl = false; 
     try 
     { 

      progress(); 
      client.Send(mail); 

     } 

     catch (Exception ex) 
     { 
      ProgressBar1.Visible = false; 
      timer1.Enabled = false; 
      Exception excpt = ex; 
      string errorMessage = string.Empty; 

      while (excpt != null) 
      { 

       errorMessage += excpt.ToString(); excpt = excpt.InnerException; 
       log.Error("Email - LMS Application Error", ex); 
       lblError.Text = "There was an error occured while processing your request.\n Please see Event Viewer for more details."; 
       lblError.ForeColor = System.Drawing.Color.Red; 
      } 
     } 
    } 
} 
+0

,現在我收到5封電子郵件,但是附件都附在電子郵件中。我的問題是每個收件人只能收到其記錄中列出的附件。例如,如果[email protected]有1.pdf的提示,他應該只接收1.pdf,其他文件不會附加。 – Dhenn 2012-03-21 00:39:42

+0

@Dhenn使用上面Ergwun的回答。它看起來不錯,他擊敗了我 – Robbie 2012-03-21 00:47:17

+0

他刪除了foreach循環,它的工作,但它只發送1附件的電子郵件.. – Dhenn 2012-03-21 00:50:17

1

你有兩個環,一個用於發送者(5)的列表和一個附件(大概3)的名單,總共15執行發送電子郵件的代碼。我會重新組織你的sendmail方法是這樣的:

foreach (string sendTo in emailList) 
{ 
    // construct email fields ... 
    // ... 

    foreach (string sendAttachments in attachList) 
    { 
     mail.Attachments.Add(new Attachment(sendAttachments)); 
    } 

    // send email ... 
    // ... 
} 
+0

所以如何糾正它? – Dhenn 2012-03-21 01:05:32

+0

根據您對其他人的回答的評論,看起來您可能會在查詢中獲得多個附件。你需要調試你的代碼,並確切地看到你從數據庫中獲得的東西。 – 2012-03-21 01:15:56

0
 private void sendMail() 
     { 

      string from="[email protected]"; 
      foreach (string sendTo in emailList) 
      { 

       foreach (string sendAttachments in attachList) 
       { 

你迭代attachList,但我什麼也看不到在這個循環中,限制attachList到僅用於收件人sendTo附件。

0

不知道爲什麼你需要一個數組列表和循環Sendmail中的方法,也許是這樣的:

/// ...snip 
    email = getEmail.GetString(0); 
    attachment = getEmail.GetString(1); 
    SendOneMail(email, attachment); 
    /// ... snip 


    private void SendOneMail(string email, string attach) 
    { 
    /// send yust one mail 
    } 
相關問題