Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a mailer in which i am sending a html page as the mail body.I have used radio buttons in order to retain the list of recipients of the mail either from database or excel worksheet or a text file. Here is the code-
C#
protected void btnSubmit_Click(object sender, EventArgs e)
        {

                      {

                SendHTMLMail();

                       }

            void SendHTMLMail()
            {

                StreamReader reader = new StreamReader(Server.MapPath("~/one.html"));
                string readFile = reader.ReadToEnd();
                Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
                string output = regx.ToString();    
                output = readFile;           
                string username = Server.UrlEncode(this.txtUsername.Text);

                output = regx.Replace(output, new MatchEvaluator((match) =>
                {

                    var url = Uri.EscapeDataString(match.Value.ToString());
                    Regex myRegex = new Regex(@"dho-.*([\d])");     
                    Match myMatch = myRegex.Match(url);
                    string Name = myMatch.Groups[0].Value;
                    return $"http://localhost:61187/two?sender={username}&link={url}&product={Name}";
                 }));


                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)
                {

                    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);


           }  } 


Now, the recipients from either database or excel or text file have some unique identity or ids. When the recipient clicks any link(as the html page has many links) in the html page that i have sent to him, i replace that link with my page link-"http://localhost:61187/two and i am passing some parameters to that page in the query string as follows-

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


Now, what i want is when the recipient clicks any link in the html page that i have sent to him, i want to retrieve his mail id. I know this can be achieved if i check for the unique id of the recipient to get his mail id. Where i am stuck is- 1. I don't know how to get the email id dynamically in the query string based on the unique id 2. How to use for loop to check for the recipient who clicked the link

What I have tried:

I know this can be achieved if i check for the unique id of the recipient to get his mail id. Where i am stuck is- 1. I don't know how to get the email id dynamically in the query string based on the unique id 2. How to use for loop to check for the recipient who clicked the link
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900