Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us
ASP.NET Tutorial Web Sites
123aspx.com
411asp.net
dotnetfreaks.com
wwwcoder.com



ASP.NET Tutorial: Send HTML Email using ASP.NET and Visual Basic.NET



Learn More about Server Intellect



Download the Visual Studio.NET 2003 VB.NET Project Here

More ASP.NET Tutorials

Sending HTML email using ASP.NET is very simple - much like sending Text email using ASP.NET

First, you will need to import the System.Text and System.Web.Mail namespace.

The System.Text namespace contains the StringBuilder Class which we will use to build our HTML email message. The System.Web.Mail namespace contains the SmtpMail Class.

Imports System.Text
Imports System.Web.Mail

We use the btnSend_Click event to do the work. Notice we first build our message using the StringBuilder Class. We then call the needed variables from the form fields in our ASP.NET page.

We then call the SmtpMail.Send to send the message.

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

Dim strMessage As New StringBuilder
Dim msg1 As New MailMessage

strMessage.Append("<table width=""75%"" border=""0"" align=""center"" cellpadding=""5"" cellspacing=""1"">")
strMessage.Append("<tr>")
strMessage.Append("<td bgcolor=""#cccccc""><strong><font color=""#000099"" size=""2"" face=""Verdana, Arial, Helvetica, sans-serif"">")
strMessage.Append(txtBody.Text) 'Get message from txtBody form field
strMessage.Append("</font></strong></td>")
strMessage.Append("</tr>")
strMessage.Append("</table>")

msg1.From = txtFrom.Text
msg1.To = txtTo.Text
msg1.Subject = txtSubject.Text
msg1.Body = strMessage.ToString
msg1.BodyFormat = MailFormat.Html
msg1.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "localhost" 'Your SMTP Server
SmtpMail.Send(msg1)

lbStatus.Text = "Message Sent"

End Sub


The flow for the code behind page is as follows.

Imports System.Text
Imports System.Web.Mail

Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents lbStatus As System.Web.UI.WebControls.Label
Protected WithEvents txtTo As System.Web.UI.WebControls.TextBox
Protected WithEvents txtFrom As System.Web.UI.WebControls.TextBox
Protected WithEvents txtSubject As System.Web.UI.WebControls.TextBox
Protected WithEvents txtBody As System.Web.UI.WebControls.TextBox
Protected WithEvents btnSend As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

Dim strMessage As New StringBuilder
Dim msg1 As New MailMessage

strMessage.Append("<table width=""75%"" border=""0"" align=""center"" cellpadding=""5"" cellspacing=""1"">")
strMessage.Append("<tr>")
strMessage.Append("<td bgcolor=""#cccccc""><strong><font color=""#000099"" size=""2"" face=""Verdana, Arial, Helvetica, sans-serif"">")
strMessage.Append(txtBody.Text) 'Get message from txtBody form field
strMessage.Append("</font></strong></td>")
strMessage.Append("</tr>")
strMessage.Append("</table>")

msg1.From = txtFrom.Text
msg1.To = txtTo.Text
msg1.Subject = txtSubject.Text
msg1.Body = strMessage.ToString
msg1.BodyFormat = MailFormat.Html
msg1.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg1)

lbStatus.Text = "Message Sent"

End Sub
End Class


Download the Visual Studio.NET 2003 VB.NET Project Here

Do you have an ASP.NET Tutorial you would like to see here? Contact Us!