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.

                   <add key="Email" value=""/>                                                
                   <add key="Password" value="yourpassword"/>
                   <add key="MailServer" value=""/>                                                
                   <add key="MailPort" value="25"/>
                   <add key="IsSSLEnabled" value="true"/>

<html xmlns="">
    <title>Email for Subscription</title>
        <tr><td colspan="2"><img src="images/Logo.jpg" width="100%" height="50px"></td></tr>
                        Dear [UserName]</p>
                 <td> Thanks for your subscription</td>
             <tr><td colspan="2"><img src="images/EndLogo.jpg" width="100%" height="50px"></td></tr>

<html xmlns="">
<head id="Head1" runat="server">
    <title>Send Email using HTML template or HTML formatted email in ASP.NET</title>
    <form id="form1" runat="server">
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
            <td colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

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)
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"]);
            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());
        catch (Exception ex)
            throw ex;

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)
            Dim Msg As New MailMessage()
            Msg.From = New MailAddress(ConfigurationManager.AppSettings("Email"))
            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())
        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-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.