.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Sending html email in ASP.Net

Posted By:ASPEvil       Posted Date: July 25, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Sending HTML emails with HTML templates, in ASP.Net.

Now a days, when we register with a website, the website send us emails which confirms our registration details and also when we some promotional emails from any company and/or organization, we see the received email is just like a webpage instead of just textual contents. How this happens? This is what is called HTML email which uses HTML templates to format the email, it sends.

In this article, I am going to explain you how we can make and send this type of emails, in ASP.Net.

For doing this, we first need to design the HTML template which will be used to format the emails. What is HTML template? It is just a simple HTML page, how you want to show the page in the email inbox of the receiver as you see the format of the emails you get in your own email inbox from other websites.

So, create a HTML page first, as you want it. And while making/designing this HTML page/template, keep in mind that, if you are going to add pictures with your HTML page, then its url should be online. For example: if you picture's name is: "image1.jpg", then its url should something like: "http://www.your-website.com/image1.jpg", it is because, when the email inbox will render the html page with its viewer, then it will find the picture's online url, not any local/attached image url.

Then, decide where you will provide variable values, I mean, the values which you will change with each email while you are sending it from the ASP.Net. For example: you are sending an order confirmation email to your website's buyer, then you need to put the order id to the HTML templete from the ASP.Net and then will send it. Then you can use a format like:-

<span style="font-weight: bold">$$ORDERID$$</span>
. Here $$ORDERID$$ is the variable values which you can change from your ASP.Net. So you can use the same HTML template for different orders.

Now, you have to access that order id variable's value from the ASP.Net. To do this, use the following ASP.Net code:-

[VB.Net code]

Imports System.Net
Imports System.Web.Mail

Public Sub SendMail()

        Dim reader As New StreamReader(Server.MapPath("~/paymentConfirmationForm.html"))
        Dim readFile As String = reader.ReadToEnd()
        Dim myString As String = ""
        myString = readFile
        myString = myString.Replace("$$ORDERID$$", "" & ORDERID & "")

        Dim smtpServer As String = "your-smtp-server-url/address"
        Dim userName As String = "email which you are wanting to use to send this email"
        Dim password_email As String = "password for the email id which you are going to use to send this email"
        Dim cdoBasic As Integer = 1
        Dim cdoSendUsingPort As Integer = 2
        Dim msg As New MailMessage()
        If userName.Length > 0 Then
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer)
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25)
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort)
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic)
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName)
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password_email)
        End If

        msg.BodyFormat = System.Web.Mail.MailFormat.Html

        msg.[To] = "" & Buyer_EMAIL & ""
        msg.From = "mention the email id which you want to be displayed as the sender's email id"
        msg.Subject = "Subject of the Email"
        msg.Body = myString.ToString()

        SmtpMail.SmtpServer = smtpServer


        Catch ex As Exception
            pnl_emailSent.Visible = False
            lbl_emailError.Text = ex.Message
            lbl_emailError.Visible = True
        End Try
    End Sub

[C# code]

using System.Net;
using System.Web.Mail;

    public void SendMail() {
        try {
            StreamReader reader = new StreamReader(Server.MapPath("~/paymentConfirmationForm.html"));
            string readFile = reader.ReadToEnd();
            string myString = "";
            myString = readFile;
            myString = myString.Replace("$$ORDERID$$", ("" 
                            + (ORDERID + "")));
            string smtpServer = "your-smtp-server-url/address";
            string userName = "email which you are wanting to use to send this email";
            string password_email = "password for the email id which you are going to use to send this email";
            int cdoBasic = 1;
            int cdoSendUsingPort = 2;
            MailMessage msg = new MailMessage();
            if ((userName.Length > 0)) {
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password_email);
            msg.BodyFormat = System.Web.Mail.MailFormat.Html;
            msg.To = ("" 
                        + (Buyer_EMAIL + ""));
            msg.From = "mention the email id which you want to be displayed as the sender\'s email id";
            msg.Subject = "Subject of the Email";
            msg.Body = myString.ToString();
            SmtpMail.SmtpServer = smtpServer;
        catch (Exception ex) {
            pnl_emailSent.Visible = false;
            lbl_emailError.Text = ex.Message;
            lbl_emailError.Visible = true;

Now, take an ASP.Net button control and within its click event, write down following code:-

[VB.Net code]

Call SendMail()

[C# code]


The above ASP.Net code call the "SendMail()" Or "SendMail();" which make the whole process of sending the email done.

Now your receiver got a HTML email.


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend