Home > ASP.Net > Create and send HTML Formatted Email

Create and send HTML Formatted Email


Hi all,

Try this example to create and send HTML Formatted Email using ASP.Net

To know how to send Email using Gmail Account from here

1) Open VS 2008 and create a new Web Site

2) Add New HTML Page and inside it add the Body of your Email name it “EmailTemplate.htm”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<img src = "http://www.mywebsite.com/images/Logo.png" /><br /><br />
<div style = "border-top:3px solid #22BCE5">&nbsp;</div>
<span style = "font-family:Arial;font-size:10pt">
Hello <b>{UserName}</b>,<br /><br />
Welcome in our Web Site.<br /><br />
<a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
<br /><br />
Thanks<br />
Website Admin
</span>
</body>
</html>

3) Add new Web Page

add Button Control and in code behind add this Code :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void SendEmail(object sender, EventArgs e)
    {
        string body = this.PopulateBody("Yasser Zaid",
            "Check Our Website",
            "http://www.mywebsite.com/Default.aspx");
        this.SendHtmlFormattedEmail("recipient@gmail.com", "Message from Admin!", body);
    }

    private string PopulateBody(string userName, string title, string url)
    {
        string body = string.Empty;
        using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
        {
            body = reader.ReadToEnd();
        }
        body = body.Replace("{UserName}", userName);
        body = body.Replace("{Title}", title);
        body = body.Replace("{Url}", url);
        return body;
    }

    private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
    {
        using (MailMessage mailMessage = new MailMessage())
        {
            mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;
            mailMessage.To.Add(new MailAddress(recepientEmail));
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["Host"];
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
            NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            smtp.Send(mailMessage);
        }
    }
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. June 6, 2014 at 6:26 am

    Good info. Lucky me I recently found your website
    by accident (stumbleupon). I have book-marked it for later!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: