如何在我的批量邮件发件人ASP.NET Windows应用程序上添加计数器?

时间:2016-03-26 08:52:47

标签: c# asp.net email

我已经构建了批量电子邮件发件人,但在发送完所有邮件后会显示一条消息。我想在事件中添加一个计数器,该计数器将显示一个消息框,其中包含一个在发送邮件时递增数字的Count。发送邮件后,将显示一条消息,显示已发送邮件和未发送邮件的总数。

以下是发送邮件事件的C#代码 -

private void btnSendEmail_Click(object sender, EventArgs e)
{
    string subject = txtSubject.Text;
    string message = txtMessage.Text;            
    if (!txtFile.Text.Equals(String.Empty))
    {
        if (System.IO.Directory.GetFiles(txtFile.Text).Length > 0)
        {
            foreach (string file in System.IO.Directory.GetFiles(txtFile.Text))
            {
            }
        }
        else
        {
        }
    }

    var con = "Data Source=Ashiq-pc;Initial Catalog=OfferMails;Integrated Security=True;MultipleActiveResultSets=True";
    List<EmailModel> emailList = new List<EmailModel>();
    using (SqlConnection myConnection = new SqlConnection(con))
    {
        string oString = "Select * from tbl_MailAdd where Flag=@Flag";
        SqlCommand oCmd = new SqlCommand(oString, myConnection);
        oCmd.Parameters.AddWithValue("@Flag", true);     
        myConnection.Open();
        using (SqlDataReader oReader = oCmd.ExecuteReader())
        {
            while (oReader.Read())
            {
                EmailModel emailModel = new EmailModel();
                emailModel.ID = Convert.ToInt16(oReader["ID"]);
                emailModel.EmailAdd = oReader["EmailAdd"].ToString();
                emailModel.Flag = Convert.ToBoolean(oReader["Flag"]);
                emailList.Add(emailModel);                 
            }

            myConnection.Close();
        }               
    }

    //return matchingPerson;
    foreach (EmailModel email in emailList)
    {
        try
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Port = 587;
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("my mail", "my pass");
            MailMessage msg = new MailMessage();
            msg.To.Add(email.EmailAdd);
            msg.From = new MailAddress("my from name");
            msg.Subject = subject;
            msg.Body = message;
            if (!txtFile.Text.Equals(String.Empty))
            {
                if (System.IO.Directory.GetFiles(txtFile.Text).Length > 0)
                {
                    foreach (string file in System.IO.Directory.GetFiles(txtFile.Text))
                    {
                        //Add file in ListBox.
                        listAttch.Items.Add(file);
                        //System.Windows.Forms.MessageBox.Show("Files found: " + file, "Message");
                        Attachment data = new Attachment(file);
                        msg.Attachments.Add(data);
                    }
                }
                else
                {
                    //listBox1.Items.Add(String.Format(“No files Found at location : {0}”, textBox1.Text));
                }
            }
            //Attachment data = new Attachment(textBox_Attachment.Text);
            //msg.Attachments.Add(data);
            client.Send(msg);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    //for (int i = 0; i < emailList.Count; i++)
    //{
    //    MessageBox.Show("i++");
    //}

    MessageBox.Show("Successfully Sent Message.");
}

1 个答案:

答案 0 :(得分:0)

如果出现立即错误,SmtpClient.Send()将抛出异常。没有办法跟踪&#34;跟踪&#34;电子邮件(除非有点击或其他的确认链接)。在收到邮件之前,您不会保持服务器连接,直到您的smtp服务器成功通过它(或者这样做失败)。

因此,在主foreach循环外添加一个简单的int counter = 0;,并在每次调用smtpclient.Send()后递增

bool Sent添加EmailModel字段。每次调用smtpclient.Send()后,请将此更新为true。

然后计算Sent字段设置为true的EMailModel的数量。

public class EMailModel
{
  int Id;
  int EmailAdd;
  bool Flag;
  bool Sent;
}

...

foreach (EmailModel email in emailList)
{
  ...
  client.Send(msg);
  email.Sent = true;
}
...
int noOfUnsentEmails = emailList.Count(x => x.Sent == false);
int noOfSentEmails = emailList.Count(x => x.Sent == true);