跟踪接收发送到批量电子邮件ID的邮件的收件人数量

时间:2018-08-13 10:26:07

标签: c# asp.net smtp radio-button

我制作了一个邮件程序,并使用smtp发送电子邮件,其中有三个单选框按钮,用于选择数据源,该数据源包含将向其发送电子邮件的收件人的列表。这是代码-

 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            {
                SendHTMLMail();

            }


now, suppose there are around say 500/1000 recipients of my mail, then how can i find the number of recipients to who my mail was successfully delivered 

            void SendHTMLMail()
            {  StreamReader reader = new StreamReader(Server.MapPath("~/index1.html"));
            string readFile = reader.ReadToEnd();
            string myString ;
            myString = readFile;

                MailMessage Msg = new MailMessage();  

                Msg.From = new MailAddress(txtUsername.Text);


                Msg.Subject = txtSubject.Text; 
                Msg.Body = mystring.ToString();
                Msg.IsBodyHtml = true;

                if (fuAttachment.HasFile)          
                {
                    string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);

                    Msg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));    
                }
                if (RadioButton1.Checked)
                {
                    sql.Open();
                    string s = "select * from address";
                    SqlCommand t = new SqlCommand(s, sql);
                    t.ExecuteNonQuery();
                    sql.Close();

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT address FROM address1";
                    cmd.Connection = sql;
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    sql.Open();
                    da.Fill(dt);
                    sql.Close();

                    foreach (DataRow row in dt.Rows)
                    {

                        Msg.To.Add(row["address"].ToString());
                    }


                }
                else if (RadioButton2.Checked)
                {
                    string connectionString = "";
                    if (FileUpload1.HasFile)
                    {
                        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                        string fileLocation = Server.MapPath("~/App_Data/" + fileName);
                        FileUpload1.SaveAs(fileLocation);

                        if (fileExtension == ".xls")
                        {

                            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                        }    
                        else if (fileExtension == ".xlsx")
                        {
                            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                        }
                        OleDbConnection con = new OleDbConnection(connectionString);
                        OleDbCommand cmd = new OleDbCommand();
                        cmd.CommandType = System.Data.CommandType.Text;
                        cmd.Connection = con;
                        OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        con.Open();
                        DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                        cmd.CommandText = "SELECT address FROM [" + getExcelSheetName + "]";
                        dAdapter.SelectCommand = cmd;
                        dAdapter.Fill(dt);
                        con.Close();


                        foreach (DataRow row in dt.Rows)
                        {

                            Msg.To.Add(row["address"].ToString());
                        }
                    }
                }
                else if (RadioButton3.Checked)
                {
                    if (FileUpload2.HasFile)
                    {
                        string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);

                        string fileLocation = Server.MapPath("~/App_Data/" + fileName);
                        FileUpload2.SaveAs(fileLocation);
                        StreamReader sr = new StreamReader(fileLocation);
                        String line = sr.ReadToEnd();
                        string[] toAddressArray;
                        toAddressArray = line.Split(new char[] { ' ' });
                        foreach (string a in toAddressArray)
                        {
                            Msg.To.Add(a);

                        }
                    }
                }

                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com"; 
                    smtp.Port = 587;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
                    smtp.EnableSsl = true;
                    smtp.Send(Msg);   
                    Msg = null;
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);


                }

            }

现在,假设我的邮件有500/1000个收件人,那么我如何找到成功接收我的邮件的收件人数量。通过这个,我的意思是假设我想向500个人发送邮件,并且在发送时,它在第231个接收方之后停止。那么有没有办法追踪它?

1 个答案:

答案 0 :(得分:1)

如果未成功发送消息,您的SmtpMail.Send(message)将返回错误。选中this以查看说明和简单示例。

因此,我认为您可以检查是否引发了任何异常,如果没有,则可以将其视为“电子邮件发送成功”。

// Request both failure and success report
Msg.DeliveryNotification = DeliveryNotificationOptions.OnFailure | 
DeliveryNotificationOptions.OnSuccess; 

int emailsSent = 0;

try 
{ 
     Console.WriteLine("start to send email ..."); 
     smtp.Send(Msg); 
     emailsSent++;
     Console.WriteLine("email was sent successfully!");

} 
catch (Exception ex) 
{ 
     Console.WriteLine("failed to send email with the following error:"); 
     Console.WriteLine(ex.Message); 
}