C# ReSend email with details and link after adding record by user

Last Reply on Apr 05, 2016 02:27 AM By dharmendr

Posted on Apr 04, 2016 02:24 AM

Hello

Into below code which is working fine, but the last section which started from using (SqlCommand SendMailcmd ... Need a help with it to update it as i want when the user click on the button the code take the AdsID and (QAdsEmail "the user email") and send an e-mail to user depending on QAdsEmail and inside the email create a link to http://www.berava.com/EditAds.aspx?AID="here AdsID" so the user when he receive the mail he can click on it and move to the page to edit his Ad info

The table where below info will store in is called "ads" and the column that's need for sending email with ad info

Note: the E-mail part of the code below i just copy it from another part of my application so that what i am looking for it some one help me to re-code it to meet the requirement above.

 

 

protected void adNewQdadsbtn_Click(object sender, EventArgs e)
        {
            string FileExtentio = System.IO.Path.GetExtension(RegInteFileUploadImg1.FileName);
            string FileExtentio2 = System.IO.Path.GetExtension(RegInteFileUploadImg2.FileName);
            string FileExtentio3 = System.IO.Path.GetExtension(RegInteFileUploadImg3.FileName);
            string FileExtentio4 = System.IO.Path.GetExtension(RegInteFileUploadImg4.FileName);
            string FileExtentio5 = System.IO.Path.GetExtension(RegInteFileUploadImg5.FileName);


            string makervalue = string.Empty;
            string Yearvalue = string.Empty;
            string Gearvalue = string.Empty;
            string RoomNum = string.Empty;
            string BathNum = string.Empty;
            string garageNum = string.Empty;
            string areasize = string.Empty;
            string QUAD = "Q";
            string AdsAprov = "N";
            string NyAdsDesc = TextBox2.Text;

            HttpCookie cookie = Request.Cookies.Get("Location");
            string Location = string.Empty;
            Location = cookie.Value;


                SqlConnection cn = new SqlConnection(sc);


                    if (RegInteFileUploadImg1.HasFile || RegInteFileUploadImg2.HasFile || RegInteFileUploadImg3.HasFile || RegInteFileUploadImg4.HasFile || RegInteFileUploadImg5.HasFile)
                    {


                        var files = new[] { FileExtentio, FileExtentio2, FileExtentio3, FileExtentio4, FileExtentio5 };
                        files = files.Where(s => !string.IsNullOrEmpty(s)).ToArray();
                        var extensions = new[] { ".jpg", ".png" };
                        if ((files.Except(extensions).Count()) <= 0)
                        {
                            if (RegDropDownList3.SelectedValue == "no")
                            {
                                AdsWrngPanel.Visible = true;
                                adsstutslbel.Text = "- Please select ads status";
                            }
                            else
                            {

                            }


                            if (DropDownList11.SelectedValue == "no")
                            {
                                AdsWrngPanel.Visible = true;
                                adscondlbel.Text = "- Please select ads condition";
                            }


                            string sqlstatment = @"INSERT INTO ads (Section, Category, AdsTit, AdsDesc, Country, State,AdsDate,
            City, AdsPrice, Img1, img2, img3, img4,img5, Wtags, QAdsTeleNum,Condition, Status,Maker,Year,Gear,RoomNo,
            Space,Shower,Garage,Currency,extlink,AdType,approvAds,QAdsEmail)
            VALUES (@Section, @Category, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Img1, @img2, @img3,
            @img4, @img5, @Wtags, @QAdsTeleNum,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage,@Currency, @extlink,@AdType,@approvAds,@QAdsEmail)";

                            SqlCommand cmd = new SqlCommand(sqlstatment, cn);


                            String shw = "Y";
                            cmd.Parameters.AddWithValue("@Section", RegSecdrdoads.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@Maker", makervalue);
                            cmd.Parameters.AddWithValue("@Year", Yearvalue);
                            cmd.Parameters.AddWithValue("@Gear", Gearvalue);
                            cmd.Parameters.AddWithValue("@RoomNo", RoomNum);
                            cmd.Parameters.AddWithValue("@Space", areasize);
                            cmd.Parameters.AddWithValue("@Category", RegCatedrdoads.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
                            cmd.Parameters.AddWithValue("@AdsDesc", NyAdsDesc);
                            cmd.Parameters.AddWithValue("@Country", cookie.Value);
                            cmd.Parameters.AddWithValue("@State", Regstatedrdolst.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@City", Regcitiesdrdolst.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
                            cmd.Parameters.AddWithValue("@QAdsTeleNum", QuickAdsMobNumTxtBox.Text);
                            cmd.Parameters.AddWithValue("@Shower", BathNum);
                            cmd.Parameters.AddWithValue("@Garage", garageNum);
                            cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);
                            cmd.Parameters.AddWithValue("@Condition", DropDownList11.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@Status", DropDownList13.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@adsDate", DateTime.Now);
                            cmd.Parameters.AddWithValue("@Currency", AddAdsCurencyLBL.Text);
                            cmd.Parameters.AddWithValue("@extlink", extlinktxtbox.Text);
                            cmd.Parameters.AddWithValue("@AdType", QUAD);
                            cmd.Parameters.AddWithValue("@approvAds", AdsAprov);
                            cmd.Parameters.AddWithValue("@QAdsEmail", QuickAdsEmailTxtBox.Text);

                            string imgnouser = "/images/general/nouser.jpg";

                            if (RegInteFileUploadImg1.HasFile)
                            {
                                cmd.Parameters.AddWithValue("@Img1", RegInteFileUploadImg1.FileName);
                                RegInteFileUploadImg1.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg1.FileName));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@Img1", imgnouser);
                            }


                            if (RegInteFileUploadImg2.HasFile)
                            {
                                cmd.Parameters.AddWithValue("@Img2", RegInteFileUploadImg2.FileName);
                                RegInteFileUploadImg2.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg2.FileName));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@Img2", imgnouser);
                            }


                            if (RegInteFileUploadImg3.HasFile)
                            {
                                cmd.Parameters.AddWithValue("@Img3", RegInteFileUploadImg3.FileName);
                                RegInteFileUploadImg3.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg3.FileName));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@Img3", imgnouser);
                            }


                            if (RegInteFileUploadImg4.HasFile)
                            {
                                cmd.Parameters.AddWithValue("@Img4", RegInteFileUploadImg4.FileName);
                                RegInteFileUploadImg4.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg4.FileName));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@Img4", imgnouser);
                            }


                            if (RegInteFileUploadImg5.HasFile)
                            {
                                cmd.Parameters.AddWithValue("@Img5", RegInteFileUploadImg5.FileName);
                                RegInteFileUploadImg5.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg5.FileName));
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@Img5", imgnouser);
                            }


                            cn.Open();
                            cmd.ExecuteNonQuery();
                            Response.Redirect("User panel.aspx");

                        }

                        else
                        {
                            AddNwAddsWrngFrmtLbl.Text = "Error: The file should have .png or .jpg format only";
                            AddNwAddsWrngFrmtLbl.ForeColor = System.Drawing.Color.Red;

                            return;
                        }



                    }

                    else
                    {
                        string sqlstatment2 = @"INSERT INTO ads (Section, Category, AdsTit, AdsDesc, Country, State,AdsDate,
            City, AdsPrice, Wtags, QAdsTeleNum,Condition, Status,Maker,Year,Gear,RoomNo, Space,Shower,Garage,extlink,AdType,approvAds,QAdsEmail)
            VALUES (@Section, @Category, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Wtags, @QAdsTeleNum,@Condition, 
@Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage, @extlink,@AdType,@approvAds,@QAdsEmail)";


                        SqlCommand cmd = new SqlCommand(sqlstatment2, cn);

                        String SHO = "Y";
                        cmd.Parameters.AddWithValue("@Section", RegSecdrdoads.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@Maker", makervalue);
                        cmd.Parameters.AddWithValue("@Year", Yearvalue);
                        cmd.Parameters.AddWithValue("@Gear", Gearvalue);
                        cmd.Parameters.AddWithValue("@RoomNo", RoomNum);
                        cmd.Parameters.AddWithValue("@Space", areasize);
                        cmd.Parameters.AddWithValue("@Category", RegCatedrdoads.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
                        cmd.Parameters.AddWithValue("@AdsDesc", NyAdsDesc);
                        cmd.Parameters.AddWithValue("@Country", cookie.Value);
                        cmd.Parameters.AddWithValue("@State", Regstatedrdolst.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@City", Regcitiesdrdolst.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
                        cmd.Parameters.AddWithValue("@QAdsTeleNum", QuickAdsMobNumTxtBox.Text);
                        cmd.Parameters.AddWithValue("@Shower", BathNum);
                        cmd.Parameters.AddWithValue("@Garage", garageNum);
                        cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);
                        cmd.Parameters.AddWithValue("@Condition", DropDownList11.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@Status", DropDownList13.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@adsDate", DateTime.Now);
                        cmd.Parameters.AddWithValue("@Currency", AddAdsCurencyLBL.Text);
                        cmd.Parameters.AddWithValue("@extlink", extlinktxtbox.Text);
                        cmd.Parameters.AddWithValue("@AdType", QUAD);
                        cmd.Parameters.AddWithValue("@approvAds", AdsAprov);
                        cmd.Parameters.AddWithValue("@QAdsEmail", QuickAdsEmailTxtBox.Text);


                        cn.Open();
                        cmd.ExecuteNonQuery();
                        Response.Redirect("User panel.aspx");
                    }



                    string userEmail = string.Empty;
                    string ADSID = string.Empty;


                    using (SqlCommand SendMailcmd = new SqlCommand("SELECT QAdsEmail, AdsID FROM ads WHERE Email = @Email"))
                    {
                        SendMailcmd.Parameters.AddWithValue("@Email", QuickAdsEmailTxtBox.Text.Trim());
                        SendMailcmd.Connection = cn;
                        cn.Open();
                        using (SqlDataReader sdr = SendMailcmd.ExecuteReader())
                        {
                            if (sdr.Read())
                            {
                                username = sdr["UID"].ToString();
                                password = sdr["Password"].ToString();
                            }
                        }
                        cn.Close();
                    }

                    if (!string.IsNullOrEmpty(password))
                    {

                        MailMessage mm = new MailMessage("autoreply@berava.com", QuickAdsEmailTxtBox.Text.Trim());
                        mm.Subject = "Welcome to Berava";
                        mm.Body = string.Format("Hello {0},<br /><br />We are so happy using Berava, now you can edit or delete your ads by click on below link:<br />HERE ENTER THE LINK<br /><br />If you have any inquiries please do not hesitate to contact us via our contact form at Berava.com, or you can send it on: info@berava.com<br /><br /> Have a happy time with Berava.<br /><br />Berava team<br /><br /><img src='http://www.berava.com/images/general/logo.png'>", username, password);
                        mm.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtpout.secureserver.net";
                        smtp.EnableSsl = false;
                        NetworkCredential NetworkCred = new NetworkCredential();
                        NetworkCred.UserName = "autoreply@berava.com";
                        NetworkCred.Password = "PASS";
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = NetworkCred;
                        smtp.Port = 80;
                        smtp.Send(mm);

                    }


        }

 

Posted on Apr 04, 2016 02:48 AM Modified on on Apr 04, 2016 02:49 AM

Hi msimoo,

Replace the below line of code

From

msimoo says:
string userEmail = string.Empty;
                    string ADSID = string.Empty;
 
 
                    using (SqlCommand SendMailcmd = new SqlCommand("SELECT QAdsEmail, AdsID FROM ads WHERE Email = @Email"))
                    {
                        SendMailcmd.Parameters.AddWithValue("@Email", QuickAdsEmailTxtBox.Text.Trim());
                        SendMailcmd.Connection = cn;
                        cn.Open();
                        using (SqlDataReader sdr = SendMailcmd.ExecuteReader())
                        {
                            if (sdr.Read())
                            {
                                username = sdr["UID"].ToString();
                                password = sdr["Password"].ToString();
                            }
                        }
                        cn.Close();
                    }
 
                    if (!string.IsNullOrEmpty(password))
                    {
 
                        MailMessage mm = new MailMessage("autoreply@berava.com", QuickAdsEmailTxtBox.Text.Trim());
                        mm.Subject = "Welcome to Berava";
                        mm.Body = string.Format("Hello {0},<br /><br />We are so happy using Berava, now you can edit or delete your ads by click on below link:<br />HERE ENTER THE LINK<br /><br />If you have any inquiries please do not hesitate to contact us via our contact form at Berava.com, or you can send it on: info@berava.com<br /><br /> Have a happy time with Berava.<br /><br />Berava team<br /><br /><img src='http://www.berava.com/images/general/logo.png'>", username, password);
                        mm.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtpout.secureserver.net";
                        smtp.EnableSsl = false;
                        NetworkCredential NetworkCred = new NetworkCredential();
                        NetworkCred.UserName = "autoreply@berava.com";
                        NetworkCred.Password = "PASS";
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = NetworkCred;
                        smtp.Port = 80;
                        smtp.Send(mm);
 
                    }

 To

string userEmail = string.Empty;
string ADSID = string.Empty; 
using (SqlCommand SendMailcmd = new SqlCommand("SELECT QAdsEmail, AdsID FROM ads WHERE Email = @Email"))
{
    SendMailcmd.Parameters.AddWithValue("@Email", QuickAdsEmailTxtBox.Text.Trim());
    SendMailcmd.Connection = cn;
    cn.Open();
    using (SqlDataReader sdr = SendMailcmd.ExecuteReader())
    {
        if (sdr.Read())
        {
            userEmail = sdr["QAdsEmail"].ToString();
            ADSID = sdr["AdsID"].ToString();
        }
    }
    cn.Close();
}
 
if (!string.IsNullOrEmpty(ADSID))
{
 
    MailMessage mm = new MailMessage("autoreply@berava.com", QuickAdsEmailTxtBox.Text.Trim());
    mm.Subject = "Welcome to Berava";
    string body = "Hello " + userEmail + ",";
    body += "<br /><br />We are so happy using Berava, now you can edit or delete your ads by click on below link:";
    body += "<br /><a href = '" + "http://www.berava.com/EditAds.aspx?AID=" + ADSID + "'>Click here to edit or delete.</a>";
    body += "<br /><br />If you have any inquiries please do not hesitate to contact us via our contact form at Berava.com, or you can send it on: info@berava.com<br /><br /> Have a happy time with Berava.<br /><br />Berava team<br /><br /><img src='http://www.berava.com/images/general/logo.png'>";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtpout.secureserver.net";
    smtp.EnableSsl = false;
    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "autoreply@berava.com";
    NetworkCred.Password = "PASS";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 80;
    smtp.Send(mm); 
}

 

 


Posted on Apr 04, 2016 02:27 PM Modified on on Apr 04, 2016 06:23 PM

Hi @dharmendr

Many thanks for your help, but for some reasons i dont know what they are it doesnt work. Its not from email server becuase i use below code in another page which is working fine

 

try
            {
              
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress("info@berava.com");
            mailMsg.To.Add("info@berava.com");
            mailMsg.Subject = msgtitltxtbx.Text;
            mailMsg.Body = "<br>Sender Name: </b>" + nametxtbx.Text + "<br/>"
                    + "<b>Sender Email : </b>" + nametxtbx.Text + "</br>"
                    + messagetxtbx.Text;
            mailMsg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtpout.secureserver.net";
            smtp.EnableSsl = false;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "info@berava.com";
            NetworkCred.Password = "pass";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 80;
            smtp.Send(mailMsg);

             conflbl.ForeColor = System.Drawing.Color.Green;
             conflbl.Text = "Thank you, your message has been successfully sent to us";

                nametxtbx.Text = "";
                txtbxemail.Text = "";
                msgtitltxtbx.Text = "";
                messagetxtbx.Text = "";

                
            }

             catch 
            {
                conflbl.ForeColor = System.Drawing.Color.Green;
                conflbl.Text = "Sorry, there is unkown problem, please try later or send your message to: info@berava.com";
            }
            
      
           
        }

 


Posted on Apr 05, 2016 02:27 AM

Hi msimoo,

As you are using godaddy account try to change the port no to default i.e. 25. Else refer the below link.

Sending email using a godaddy account

Still if your problem not resolve then You need to contact GoDaddy Support and get proper email settings for your account.