Send a Plain Text and HTML newsletter using System.Net.Mail namespace

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” “”&gt;


<html xmlns=”; >

<head runat=”server”>

    <title>Send a Plain Text and HTML form</title>



    <form id=”form1″ runat=”server”>

    <asp:Label ID=”label1″ runat=”server”></asp:Label>

    <table border=”1″ width=”505px”>


            <td style=’width: 7px’>



                <asp:TextBox ID=”txtFrom” runat=”server” Width=”155px”></asp:TextBox></td>   



            <td style=’width: 7px’>



                <asp:TextBox ID=”txtTo” runat=”server” Width=”157px”></asp:TextBox></td>   



            <td style=’width: 7px; height: 23px’>


            <td style=’height: 23px’>

                <asp:TextBox ID=”txtSubject” runat=”server” Width=”505px”></asp:TextBox></td>   



            <td colspan=”2″>

                TEXT Version Paste or type in text</td>   



            <td colspan=”2″>

            <asp:TextBox ID=”txtText” TextMode=”MultiLine” Columns=”20″ Rows=”20″ runat=”server” Width=”556px” Height=”202px”>

This is the Text version





            <td colspan=”2″>

                HTML Version Paste or type in HTML formatted text</td>   



            <td colspan=”2″>

            <asp:TextBox ID=”txtHTML” TextMode=”MultiLine” Columns=”20″ Rows=”20″ runat=”server” Width=”559px” Height=”249px”>


     <h2>This is the HTML version</h2>






        <asp:Button ID=”Button1″ runat=”server” Text=”Button” />






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())



        ‘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





        ‘Send the message

        Dim smtp As New System.Net.Mail.SmtpClient(“”) ‘specify the mail server address


        label1.Text = “<b>Message Sent:” & System.DateTime.Now() & “</b>”


    End Sub ‘MultiPartMime   

End Class


Reference Links




·        PINE Text based email client.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: