Send email using an Email Template as Body in ASP.Net


In this article, I will explain how to send emails using an Email Template as Body in ASP.Net.

HTML Markup

The HTML Markup has a form with some fields such as Recipient Email address and Subject and you can add your own fields as per your requirements.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendEmail.aspx.cs" Inherits="ASPShortCodes.SendEmail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">
            To:
        </td>
        <td>
            <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
    </tr>
  
    <tr>
        <td>
        </td>
        <td>
          <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" />
        </td>
    </tr>
</table>

            
        </div>
    </form>
</body>
</html>


Namespaces

You will need to import the following namespaces

C#


using System;
using System.IO;
using System.Net;
using System.Net.Mail;


C#

The below code will send an email to recipients.


 protected void Button1_Click(object sender, EventArgs e)
        {
           try
            {
                using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {

                    {
                        MailMessage msg = new MailMessage();
                        msg.From = new MailAddress("shabbcs@gmail.com");

                        msg.Bcc.Add("test1@gmail.com");
                        msg.CC.Add("test2@gmail.com");

                     
                        msg.To.Add(txtTo.Text);
                        
                        msg.Subject = txtSubject.Text;

                        string Link = "#add your URL";
                        string body = this.createEmailBody();
                        msg.Body = body.Replace("Respond Here", "<br/><a href=" + Link + ">Respond Here<a>");
                        msg.IsBodyHtml = true;
                                                               
                                              
                        smtp.Host = "smtp.gmail.com";
                        smtp.EnableSsl = true;
                        NetworkCredential NetworkCred = new NetworkCredential("shabbcs@gmail.com", "xxxxxxxx");
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = NetworkCred;
                        smtp.Port = 587;
                        smtp.Send(msg);
                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);

                        
                    }
                }
            }
           catch { }
        }


In the string variable Link you can assign your text to be replaced in the template body.

And the below function will create an email body from the Email Template.



 private string createEmailBody()
        {

            string body = string.Empty;
            //using streamreader for reading my htmltemplate   

            using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.html")))
            {

                body = reader.ReadToEnd();

            }


            return body;

        }


EmailTemplate.html

Create your Email Body template in the HTML page.


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <b>Hi</b>
    Respond Here
</body>
</html>



Please comment if you need any other information.


Thank You
Shabbeer

0 Comments