使用数据库中的附件路径发送批量电子邮件

时间:2012-03-21 00:14:57

标签: c# winforms

我只是需要帮助。基本上我正在创建一个Windows应用程序,它可以向我们的客户发送批量电子邮件。字段“email”和“attachment”来自数据库。附件字段仅包含文件所在的路径,代码正在运行,但我收到了15封电子邮件,而不是收到5封电子邮件。

注意:我的数据库只包含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="myemail@email.com";
                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;
                            }
                        }

                    }
                }
            }

5 个答案:

答案 0 :(得分:4)

看起来您正在从MEMBERREQUIREMENTS表中提取一堆包含电子邮件地址和附件路径的记录,并构建电子邮件地址和附件路径列表。

然后,您将迭代这些发送,为每个地址和附件组合发送电子邮件。我猜这不是你想要做的。 我想是这样的:

  • 每个收件人只应收到其记录中列出的附件。在这种情况下,您不希望迭代每个地址的附件,或者可能
  • 每个收件人都应该收到每个附件,但附件应该列在一个单独的表中,而不是存储在MEMBERREQUIREMENTS表中每个地址的记录中。

<强>更新

下面是一些代码来做前者。请注意,这只是对已发布代码的最小编辑,并不修复变量名称等,或提供任何其他错误检查(您可能想要这样做):

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

答案 1 :(得分: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 ...
    // ...
}

答案 2 :(得分:0)

这是因为您已经在附件foreach循环中调用了client.Send(mail);。它应该在那个循环之外。

尝试将sendMail方法更改为:

private void sendMail()
{
    string from = "myemail@email.com";
    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;
            }
        }
    }
}

答案 3 :(得分:0)

        private void sendMail()
        {

            string from="myemail@email.com";
            foreach (string sendTo in emailList)
            {

                foreach (string sendAttachments in attachList)
                {

您正在对attachList进行迭代,但我在此循环中看不到将attachList限制为仅适用于收件人sendTo的附件。

答案 4 :(得分: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
  }