2016-03-26 4 views
0

私は大量のメール送信者を作成しましたが、すべてのメールを送信した後にメッセージを表示します。私は、メールを送信している間に番号をインクリメントするメッセージボックスを表示するイベントにカウンタを追加したいと思います。メールを送信した後に、送信されたメールと未送信のメールの総数がメッセージに表示されます。ここでバルクメール送信者のASP.NET Windows Appにカウンタを追加するには?

は、送信メールのイベントのための私の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 [email protected]"; 
     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."); 
} 

答えて

0

即時エラー、SmtpClient.Sendが(がある場合)は、例外がスローされます。電子メールを「追跡」する方法はありません(クリックするなどの確認リンクがない限り)。 smtpサーバが正常にサーバを通過する(または失敗した)場合にのみ、メールが受信されるまでサーバ接続を維持しません。

smtpclient.Send()

または

への各呼び出しは、あなたのEmailModelbool Sentフィールドを追加した後、だからあなたのメインのforeachループと増分外シンプルint counter = 0;を追加します。 smtpclient.Send()を呼び出すたびに、これをtrueに更新します。

その後、送信フィールドが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); 
+0

「数字」メールが送信されたことを示すカウンタをコード化してください。 – Ashiq

関連する問題