Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit HTML Email template at run time in .net code

I below code is to send email by reading the template html, and it works fine. But now my question is how to pass Salutation customerName from my .net code to the template at run time.

 StringBuilder strBlr = new StringBuilder();
            string strHTML = string.Empty;
            string strTempalteHtmlpath = string.Empty;

            //create the mail message
            MailMessage mail;

 string strFrom = ConfigurationSettings.AppSettings["fromAddressForBT"];
                string strSubject = "Thanks for choosing Email contact preference";
                mail = new MailMessage(strFrom, customerDetails.EmailId);

                mail.Subject = strSubject;

                //Read Html Template File Path
                strTempalteHtmlpath = Convert.ToString(ConfigurationSettings.AppSettings["TemplatePath"]);
                strHTML = File.ReadAllText(strTempalteHtmlpath);
                strBlr = strBlr.Append(strHTML);
                mail.Body = strBlr.ToString();
                mail.IsBodyHtml = true;


                //first we create the Plain Text part
                AlternateView plainView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/plain");
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/html");

 mail.AlternateViews.Add(plainView);
                mail.AlternateViews.Add(htmlView);

                //send the message
                SmtpClient smtpMail = new SmtpClient(ConfigurationSettings.AppSettings["smtpClient"]);
                smtpMail.Send(mail);

                mail.Dispose();

Thanks.

like image 451
Pradeep Avatar asked Dec 07 '25 01:12

Pradeep


1 Answers

This is code for sendemail button

    StreamReader sr = new StreamReader(Server.MapPath("Sendpage.htm"));
    string body = sr.ReadToEnd();         
    sr.Close();          
    body = body.Replace("#NameFamily#", txtNameFamily.Text);     
    body = body.Replace("#Email#", txtEmail.Text);        
    body = body.Replace("#Tellphone#", txtTellphone.Text);   
    body = body.Replace("#Text#", txtText.Text);       
    body = body.Replace("#Date#", DateTime.Now);        
    string Time = Convert.ToString(DateTime.Now.ToShortTimeString());    
    body = body.Replace("#Time#", Time);        
    SendMail("email that you want to send to it", body); 

this is sendmail function code:

 private void SendMail(string To, string Body)
    {
        SmtpClient Mailing = new SmtpClient("mail.domain.com");
        MailMessage Message = new MailMessage();
        Message.From = new MailAddress("[email protected]", "Your name or company name");
        Message.Subject = "Subject";
        Message.SubjectEncoding = Encoding.UTF8;
        Message.IsBodyHtml = true;
        Message.BodyEncoding = Encoding.UTF8;
        Message.Body = Body;
        Message.To.Add(new MailAddress(To));
        Mailing.UseDefaultCredentials = false;
        NetworkCredential MyCredential = new NetworkCredential("[email protected]", "password");
        Mailing.Credentials = MyCredential; Mailing.Send(Message);
    }
like image 52
rohan panchal Avatar answered Dec 08 '25 15:12

rohan panchal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!