email_html.aspx
<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Net.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim strEmail As String
Dim myMessage As New MailMessage
Dim mySmtpClient As SmtpClient
If Page.IsValid() Then
strEmail = txtEmail.Text
myMessage.From = New MailAddress("webmaster@asp101.com", "ASP 101 Webmaster")
myMessage.To.Add(New MailAddress(strEmail))
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"
' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.IsBodyHtml = True
' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to webmaster@asp101.com.</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail & "</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf
' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
mySmtpClient = New SmtpClient("localhost")
mySmtpClient.Send(myMessage)
frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to " & strEmail & "."
End If
End Sub
</script>
<html>
<head>
<title>ASP.NET 2.0 Email (HTML Format) Sample</title>
</head>
<body>
<asp:Label id="lblUserMessage" text="Enter your e-mail address:" runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />
<asp:Button id="btnSendMail" text="Send Mail!" OnClick="btnSendMail_OnClick" runat="server" />
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>