2017-10-02 14 views
0

10-12電子メールを送信した後にエラーを与える私は、コードを使用してSMTP電子メールを送信しています:が遅い質量SMTPメールを送信し、

protected void btnSendEmailToAll_Click(object sender, EventArgs e) 
{ 
    string username = Master.User; 

    //Create a temporary DataTable 
    DataTable dtCustomers = new DataTable(); 
    dtCustomers.Columns.AddRange(new DataColumn[3] { new DataColumn("name", typeof(string)), 
       new DataColumn("email",typeof(string)), new DataColumn("EmailSentOn",typeof(string)) }); 

    //Copy the Checked Rows to DataTable 
    foreach (GridViewRow row in Grid.Rows) 
    { 
     // Only look in data rows, ignore header and footer rows 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkrow"); 

      if (ChkBoxRows.Checked == true) 
      { 
       string Name = (row.FindControl("lblname") as Label).Text.ToLower(); 

       dtCustomers.Rows.Add(Name, (row.FindControl("lblemail") as Label).Text, (row.FindControl("lblEmailsentOn") as Label).Text); 

       var id = Grid.DataKeys[row.RowIndex].Value; 
       using (var db = new DatabaseHelper()) 
       { 
        db.ExecNonQuery(Queries.UpdateReminderEmailSentData, "@RW", id); 
       } 
      } 
     } 
    } 

    string subject = "My Subject"; 

    //Using Parallel Multi-Threading send multiple bulk email. 
    Parallel.ForEach(dtCustomers.AsEnumerable(), row => 
    { 
     string body = this.PopulateBody(row["name"].ToString(), row["EmailSentOn"].ToString(), username); 
     SendMassEmail(row["email"].ToString(), subject, body.ToString()); 
    }); 

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Email Sent Successfully')", true); 
    BindGrid(); 
} 

PopulateBodyののfuction:

SendMassEmail機能:

private bool SendMassEmail(string recipient, string subject, string body) 
{ 
    var smtp = new SmtpClient() 
    { 
     Host = WebConfigurationManager.AppSettings["Host"],//smtp.gmail.com 
     EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]),//true 
     UseDefaultCredentials = true, 
     Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["UserName"], WebConfigurationManager.AppSettings["Password"]), 
     Port = int.Parse(WebConfigurationManager.AppSettings["Port"])//587 
    }; 
    using (MailMessage mailMessage = new MailMessage()) 
    { 
     mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings["UserName"], "My Email"); 
     mailMessage.Subject = subject; 
     mailMessage.Body = body;      
     mailMessage.IsBodyHtml = true; 
     mailMessage.To.Add(new MailAddress(recipient)); 
     smtp.Send(mailMessage); 
    } 
    return true; 
} 

それは5つの電子メール:(

を送信するために30代を取ります速いですが、一部は未なくなって、すべての電子メールを送信しない

Thread T1 = new Thread(delegate() 
      { 
       using (MailMessage mailMessage = new MailMessage()) 
       { 
        mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings["UserName"], "My Email"); 
        mailMessage.Subject = subject; 
        mailMessage.Body = ShowBody; 
        mailMessage.IsBodyHtml = true; 
        mailMessage.To.Add(new MailAddress(recepientEmail)); 

       smtp.Send(mailMessage);     
       } 
     }); 
T1.Start(); 

を:(でも、私たちはparallel.foreachを使用している場合は、スレッドを使用する必要がないことが示唆されている)のような

私はスレッドを使用してみました。さらに少ない時々10-12電子メールを送信したりした後、それが例外をスロー

すなわち:型「System.Net.Mail.SmtpException」 の

未処理の例外がSystem.dllが発生しました。追加情報:サービスではありません利用可能な、 閉鎖伝送チャネル。サーバーの応答は:4.7.0 Temporary システムの問題でした。後でもう一度試してください(WS)。 u12sm5363862pfg.146 - gsmtp

何が間違っているかわかりません。これに関する助けをいただければ幸いです。

Plzを私は多くのソリューションを探索したが、事前に正しいone.Thanksを見つけるcoudn'tとして複製したり、何もそれをマークいけません!

+0

そうしないと、TCPプールを使用するには、手動でクローズSmtpClientにリソースの不足を避けるためにあらゆる時間を必要とする、とあなたは、マルチスレッドの数を制限する必要があります。 –

+0

@Ray Hの助けのための31人のおかげで、私は新しい午前smtp.Close()を使用し、smtp.Send後 – Preet

+0

多くを知らないこの申し訳ありませんを行う方法を教えてplzはできます。複数のスレッドを使用する場合は、スレッドプールを管理する必要がありますが、それは少し複雑です。どのような種類の大量のメールを送信するアクションは、Webアプリケーションではお勧めしません、あなたはそれを送信するキューを管理する必要があります –

答えて

0

私は私のプロジェクトでこれを使用しています。その良い仕事。 10-12個以上のメールを送信した後もエラーは表示されません。

public static void Send(string from, string to, string cc, string subject, string message, string[] attachment, byte[] fileByte, string fileByteName) 
     { 
      string ccChar; 
      if (!string.IsNullOrEmpty(cc) && cc.EndsWith(",")) 
       ccChar = cc.Remove(cc.Length - 1, 1); 
      else 
       ccChar = cc; 

      if (smtp_address == "") 
      { 
       smtp_address = ConfigurationManager.AppSettings["smtp_address"]; 
       smtp_port = int.Parse(ConfigurationManager.AppSettings["smtp_port"]); 
       smtp_user = ConfigurationManager.AppSettings["smtp_user"]; 
       smtp_password = ConfigurationManager.AppSettings["smtp_password"]; 
       smtp_ssl = bool.Parse(ConfigurationManager.AppSettings["smtp_ssl"]); 
      } 

      MailMessage mm = new MailMessage(from, to, subject, message); 
      mm.Bcc.Add(ConfigurationManager.AppSettings["mailAlertMonitor"]); 

      mm.IsBodyHtml = true; 
      if (!string.IsNullOrEmpty(ccChar)) mm.CC.Add(ccChar); 

      if (fileByte.Length != 0 && !string.IsNullOrEmpty(fileByteName)) 
      { 
       mm.Attachments.Add(new Attachment(new MemoryStream(fileByte), fileByteName)); 
      } 

      if (attachment != null) 
      { 
       foreach (string file in attachment) 
       { 
        if (!string.IsNullOrEmpty(file)) 
        { 
         Attachment attc = new Attachment(file); 
         mm.Attachments.Add(attc); 
        } 
       } 
      } 

      SmtpClient smtp = new SmtpClient(smtp_address, smtp_port); 
      smtp.Credentials = new NetworkCredential(smtp_user, smtp_password); 
      smtp.EnableSsl = smtp_ssl; 

      smtp.Send(mm); 
      InsertSendEmailLog(from, to, cc, ConfigurationManager.AppSettings["mailAlertMonitor"], subject, message); 
     } 

私はあなたを助けてくれることを願っています。

+0

のために残念である必要がありsmtp.Dispose() – Preet

0

は次のように試してみて、私に知らせてください。

 private static string smtp_address = ""; 
     private static int smtp_port = 587; 
     private static string smtp_user = ""; 
     private static string smtp_password = ""; 
     private static bool smtp_ssl = false; 
     private bool SendMassEmail(string from, string to, string cc, string subject, string message, string body) 
     { 
      smtp_address = WebConfigurationManager.AppSettings["Host"]; 
      smtp_port = int.Parse(WebConfigurationManager.AppSettings["Port"]); 
      smtp_user = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["UserName"]; 
      smtp_password = WebConfigurationManager.AppSettings["Password"]); 
      smtp_ssl = smtp_ssl; 

      MailMessage mm = new MailMessage(from, to, subject, message); 
      SmtpClient smtp = new SmtpClient(smtp_address, smtp_port); 
      smtp.Credentials = new NetworkCredential(smtp_user, smtp_password); 
      smtp.EnableSsl = smtp_ssl; 

      smtp.Send(mm); 
      return true; 
     } 
+0

おかげラスRASSが、私は自分のコードからの唯一の違いはsmtp.enableSSlが偽だと思います。それは問題を引き起こしていますか?とにかくこれを試し、あなたに知らせる – Preet

+0

私はenableAsl = falseを作るとエラーになる。私のために働いていない – Preet

関連する問題