Saturday, 22 March 2014

How to send emails using HTML Templates in ASP.NET using C#/VB.NET

Hi Friends,in this article i will explain about Send Email using HTML template or HTML formatted email in ASP.NET.
I already explained in the previous articles about How to Create Simple RDLC Report in ASP.NET,Create your own captcha image generator in ASP.NET using C#.NET/VB.NET and Filter and Sorting in GridView using DataView.

Write the following lines in web.config file.

<appSettings>
                   <add key="Email" value="example@gmail.com"/>                                                
                   <add key="Password" value="yourpassword"/>
                   <add key="MailServer" value="smtp.gmail.com"/>                                                
                   <add key="MailPort" value="25"/>
                   <add key="IsSSLEnabled" value="true"/>
          </appSettings>

SendMail.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Email for Subscription</title>
</head>
<body>
    <div>
        <table>
        <tr><td colspan="2"><img src="images/Logo.jpg" width="100%" height="50px"></td></tr>
            <tr>
                <td>
                    <p>
                        Dear [UserName]</p>
                </td>
            </tr>
            <tr>
                 <td> Thanks for your subscription</td>
                  
            </tr>
             <tr><td colspan="2"><img src="images/EndLogo.jpg" width="100%" height="50px"></td></tr>
        </table>
    </div>
</body>
</html>

EmailTemplate.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Send Email using HTML template or HTML formatted email in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
   
        <tr>
            <td>
                UserName
            </td>
           
            <td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Site
            </td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

EmailTemplate.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Net.Mail;
using System.Configuration;
using System.Text;

public partial class EmailTemplate : System.Web.UI.Page
{

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"]);
            Msg.To.Add(txtEmail.Text);
            StreamReader reader = new StreamReader(Server.MapPath("~/SendMail.htm"));
            string readFile = reader.ReadToEnd();
            string StrContent = "";
            StrContent = readFile;
            StrContent = StrContent.Replace("[UserName]", txtName.Text);
            StrContent = StrContent.Replace("[Email]", txtEmail.Text);
            Msg.Subject = " Thanks for your subscription";
            Msg.Body = StrContent.ToString();
            Msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["MailServer"];
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ConfigurationManager.AppSettings["Email"];
            NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = int.Parse(ConfigurationManager.AppSettings["MailPort"]);
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSSLEnabled"].ToString());
            smtp.Send(Msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}
EmailTemplate.aspx.vb
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Net
Imports System.IO
Imports System.Net.Mail
Imports System.Configuration
Imports System.Text

Partial Public Class EmailTemplate
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
        Try
            Dim Msg As New MailMessage()
            Msg.From = New MailAddress(ConfigurationManager.AppSettings("Email"))
            Msg.[To].Add(txtEmail.Text)
            Dim reader As New StreamReader(Server.MapPath("~/SendMail.htm"))
            Dim readFile As String = reader.ReadToEnd()
            Dim StrContent As String = ""
            StrContent = readFile
            StrContent = StrContent.Replace("[UserName]", txtName.Text)
            StrContent = StrContent.Replace("[Email]", txtEmail.Text)
            Msg.Subject = " Thanks for your subscription"
            Msg.Body = StrContent.ToString()
            Msg.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = ConfigurationManager.AppSettings("MailServer")
            Dim NetworkCred As New System.Net.NetworkCredential()
            NetworkCred.UserName = ConfigurationManager.AppSettings("Email")
            NetworkCred.Password = ConfigurationManager.AppSettings("Password")
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("MailPort"))
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings("IsSSLEnabled").ToString())
            smtp.Send(Msg)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class

The output of the above code is mail will be sent as SendMail.html text and replacing [UserName].


You can download the code by clicking on the below Download image.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.