使用SMTP发送批量电子邮件只发送少量电子邮件

时间:2017-09-08 03:14:28

标签: c# asp.net multithreading smtp bulk-email

我有一个gridview显示来自SQL table的客户的姓名,电子邮件,日期等数据。它包含checkboxes以选择所有行。点击标题checkbox后,行中的checkboxes也会被检查(工作正常)。 gridview绑定代码:

private void BindGrid()
        {
            using (var db = new DatabaseHelper())
            {
                using (var ds = db.ExecDataSet(Queries.BindReminderEmailGrid))
                {
                   Grid.DataSource = ds;
                }
            }
            Grid.DataBind();
        }

if(!IsPostBack){}上的PageLoad内调用此代码。

我点击了一个按钮,我正在向所有选中的行发送电子邮件:

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

            CheckBox ChkBoxHeader = (CheckBox)Grid.HeaderRow.FindControl("chkboxSelectAll");

            //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 (ChkBoxHeader.Checked == true)
                    {
                        ChkBoxRows.Checked = true;
                    }
                    if (ChkBoxRows.Checked == true)
                    {
                        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 = "Email Subject";

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

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

SendHTMLFormatedEmail功能:

 private void SendHtmlFormattedEmail(string recepientEmail, string subject, string ShowBody)
        {
            if (recepientEmail != "")
            {
                var smtp = new SmtpClient()
                {
                    Host = WebConfigurationManager.AppSettings["Host"],
                    EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]),                    
                    UseDefaultCredentials = true,
                    Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["UserName"], WebConfigurationManager.AppSettings["Password"]),
                    Port = int.Parse(WebConfigurationManager.AppSettings["Port"])
                };

                 using (MailMessage mailMessage = new MailMessage())
                    {
                        mailMessage.From = new MailAddress(WebConfigurationManager.AppSettings["UserName"], "Email From Us");
                        mailMessage.Subject = subject;
                        mailMessage.Body = ShowBody;

                        mailMessage.IsBodyHtml = true;
                        mailMessage.To.Add(new MailAddress(recepientEmail));

                     smtp.Send(mailMessage);
                    }

            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter a valid email address')", true);
            }
        }

一切似乎都运转良好,但问题是它只向少数客户发送电子邮件。例如,今天我向大约56位客户发送电子邮件,但实际上只发送了11封电子邮件。

编辑完成后出现以下错误:

  

服务不可用,关闭传输通道。服务器   回应是:4.7.0临时系统问题。稍后再试(WS)。   x4sm1307841pfb.101 - gsmtp

     

仅向12位客户发送电子邮件,但名称相同:(

我无法追踪它出了什么问题。请帮我解决它是如何修复的。在此先感谢.. !!

0 个答案:

没有答案