how to get the email-id of the receiver who clicks

2019-08-25 07:12发布

问题:

I have a mailer in which i have to fill the credentials in the textbox except for the recipients to whom i am sending the mail.For choosing the recipients, i have used radio buttons to select the recipients from database or an excel sheet or a text file.

 SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            getFiles();
            if (!IsPostBack)
            {
                var list = getFiles();
                dd1.DataSource = list;
                dd1.DataTextField = "Key";
                dd1.DataValueField = "Value";
                dd1.DataBind();
            }
        }  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            {
                SendHTMLMail();

            }




            void SendHTMLMail()
            {
                StreamReader reader = new StreamReader(dd1.SelectedItem.Value);
                string readFile = reader.ReadToEnd();
                Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
                string output = regx.ToString();    
                string count = 0.ToString();
                output = readFile;
                string username = Server.UrlEncode(this.txtUsername.Text);
                output = regx.Replace(output, new MatchEvaluator((match) =>
                {
                    var url = Uri.EscapeDataString(match.Value.ToString());
                    return $"http://localhost:61187/two?sender={username}&link={url}&count={count}";
                }));


                MailMessage Msg = new MailMessage();  

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


                Msg.Subject = txtSubject.Text;      
                Msg.Body = output.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);
                }   
            }

        public List<KeyValuePair<string, string>> getFiles()
        {
            DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));
            var fi = di.EnumerateFiles("*.html", SearchOption.TopDirectoryOnly);
            var files = new List<KeyValuePair<string, string>>();      
            foreach (FileInfo file in fi)
            {
                files.Add(new KeyValuePair<string, string>(file.Name, file.FullName));
            }
            return files;
        }

Now, what i want is to get the email id of the recipient who clicks the link in the html page that i have sent to him as the mail body. How to get the email id which is among one of the three radio button data sources?Also, i want to get the current date and time at which he clicks the link . I want to pass that email id and current date and time in this query string-

 return $"http://localhost:61187/two?sender={username}&link={url}&count={count}";