Send a Plain Text and HTML newsletter using System.Net.Mail namespace
This article shows how to send an email that includes a Plain Text and HTML version. The email client will display which version it is applicable. The webpage uses System.Net.Mail built into .NET 2.0 to send the email.
Deployment and Testing
· Create two files named Sendmail.aspx and Sendmail.aspx.vb
· Deploy to your website.
· Ensure the SMTP Service is installed on machine
· Open sendmail.aspx in a browser and send a test message
· Open your test message in an email client, this should display an HTML version.
· Change your email client to only view message in Text format, the ‘text’ version should display.
Sendmail.aspx webpage
<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”sendmail.aspx.vb” ValidateRequest=”false” Inherits=”_Default” %>
<!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 runat=”server”>
<title>Send a Plain Text and HTML form</title>
|</head>
<body>
<form id=”form1″ runat=”server”>
<asp:Label ID=”label1″ runat=”server”></asp:Label>
<table border=”1″ width=”505px”>
<tr>
<td style=’width: 7px’>
From</td>
<td>
<asp:TextBox ID=”txtFrom” runat=”server” Width=”155px”></asp:TextBox></td>
</tr>
<tr>
<td style=’width: 7px’>
To:</td>
<td>
<asp:TextBox ID=”txtTo” runat=”server” Width=”157px”></asp:TextBox></td>
</tr>
<tr>
<td style=’width: 7px; height: 23px’>
Subject</td>
<td style=’height: 23px’>
<asp:TextBox ID=”txtSubject” runat=”server” Width=”505px”></asp:TextBox></td>
</tr>
<tr>
<td colspan=”2″>
TEXT Version Paste or type in text</td>
</tr>
<tr>
<td colspan=”2″>
<asp:TextBox ID=”txtText” TextMode=”MultiLine” Columns=”20″ Rows=”20″ runat=”server” Width=”556px” Height=”202px”>
This is the Text version
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan=”2″>
HTML Version Paste or type in HTML formatted text</td>
</tr>
<tr>
<td colspan=”2″>
<asp:TextBox ID=”txtHTML” TextMode=”MultiLine” Columns=”20″ Rows=”20″ runat=”server” Width=”559px” Height=”249px”>
<html>
<body>
<h2>This is the HTML version</h2>
</body>
</body>
</asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” />
</form>
</body>
</html>
Sendmail.aspx.vb code behind
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
‘Create the mail message
Dim mail As New System.Net.Mail.MailMessage()
mail.BodyEncoding = Encoding.GetEncoding(“iso-8859-1”)
‘Set the addresses
mail.From = New System.Net.Mail.MailAddress(txtFrom.Text.ToString())
mail.To.Add(txtTo.Text.ToString())
‘Set the content
mail.Subject = txtSubject.Text.ToString()
‘Create the Plain Text part
Dim plainView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtText.Text.ToString(), Nothing, “text/plain”)
plainView.TransferEncoding = Net.Mime.TransferEncoding.QuotedPrintable
‘Create the Html part
Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtHTML.Text.ToString(), Nothing, “text/html”)
htmlView.TransferEncoding = Net.Mime.TransferEncoding.QuotedPrintable
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
‘Send the message
Dim smtp As New System.Net.Mail.SmtpClient(“example.com”) ‘specify the mail server address
smtp.Send(mail)
label1.Text = “<b>Message Sent:” & System.DateTime.Now() & “</b>”
End Sub ‘MultiPartMime
End Class
Reference Links
· http://msdn2.microsoft.com/en-us/library/system.net.mail.aspx
· http://www.systemnetmail.com
· PINE Text based email client. http://www.washington.edu/pine/getpine/