Asp-Net Components : Email

  1.  Send an email
  2. Simplest Way of Sending Email from Gmail
  3. Send out email in code behind (C#)
  4. Send out an Email
  5. Send email with attached file
  6. Send email out through asp:form
  7. Send email to all employees in database
  8. Send email with html form message
  9. Email message in a html format
  10. Email form with cc and bcc
  11. Email with priority
  12. Send email function
  13. Email with pictures
  14. Send email with more than one attachments
  15. Send out an email in case of page error

Send an email

<%@ Page Language="VB" %>
<script runat="server">
    
    Sub Button1_Click(sender As Object, e As EventArgs)
      Label1.Text = SendMail("Feedback form", TextBox1.Text, TextBox2.Text)
    End Sub
    
    Function SendMail(Subject As String, FromAddress As String, Message As String) As String
    
    ' Build a MailMessage
    Dim mailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
    mailMessage.From = "someone@example.com"
    mailMessage.To = "someone@example.com"
    mailMessage.Subject = "Sending an e-mail from a web page"
    mailMessage.BodyFormat = System.Web.Mail.MailFormat.Text
    
    ' TODO: Set the mailMessage.Body property
    mailMessage.Body = Message
    
    System.Web.Mail.SmtpMail.SmtpServer = "localhost"
    System.Web.Mail.SmtpMail.Send(mailMessage)
    
    SendMail = "Your message was sent to " & mailMessage.To
    
    End Function

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Your email address: 
            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            Your message: 
            <asp:TextBox id="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Send"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>

Simplest Way of Sending Email from Gmail

// Code by 'dotnetguts at gmail.com'


  protected void btnSendEmail_Click(object sender, EventArgs e)
      {
    //Create Mail Message Object with content that you want to send with mail.
          System.Net.Mail.MailMessage MyMailMessage = new         System.Net.Mail.MailMessage("dotnetguts@gmail.com","myfriend@yahoo.com", 
      "This is the mail subject", "Just wanted to say Hello");
        
          MyMailMessage.IsBodyHtml = false;
 
    //Proper Authentication Details need to be passed when sending email from gmail
          System.Net.NetworkCredential mailAuthentication = new          
    System.Net.NetworkCredential("dotnetguts@gmail.com", "myPassword");
 
    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this details changes and you can
    //get it from respective server.
          System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com",587);
 
    //Enable SSL
          mailClient.EnableSsl = true; 
    
          mailClient.UseDefaultCredentials = false;
 
          mailClient.Credentials = mailAuthentication;
 
         mailClient.Send(MyMailMessage);
      }

Send out email in code behind (C#)

<%@ Page language="c#" src="IssueReporter.aspx.cs" AutoEventWireup="false" Inherits="IssueReporter.IssueReporter" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 54px" runat="server" Font-Names="Verdana" Font-Size="X-Small">Your Name:</asp:Label>
      <asp:TextBox id="txtComment" style="Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 112px" runat="server" Width="384px" Height="112px" TextMode="MultiLine" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:TextBox id="txtSender" style="Z-INDEX: 106; LEFT: 176px; POSITION: absolute; TOP: 80px" runat="server" Width="259px" Height="24px" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 86px" runat="server" Font-Names="Verdana" Font-Size="X-Small">Your Email:</asp:Label>
      <asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 118px" runat="server" Width="104px" Height="16px" Font-Names="Verdana" Font-Size="X-Small">Comment:</asp:Label>
      <asp:CheckBox id="chkPriority" style="Z-INDEX: 104; LEFT: 32px; POSITION: absolute; TOP: 240px" runat="server" Width="416px" Height="24px" Text="Please Reply Immediately!" Font-Names="Verdana" Font-Size="X-Small"></asp:CheckBox>
      <asp:TextBox id="txtName" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 48px" runat="server" Width="258px" Height="24px" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:Button id="cmdSend" style="Z-INDEX: 108; LEFT: 32px; POSITION: absolute; TOP: 288px" runat="server" Width="88px" Height="24px" Text="Send"></asp:Button>
      <asp:Label id="lblResult" style="Z-INDEX: 109; LEFT: 40px; POSITION: absolute; TOP: 352px" runat="server" Width="432px" Height="72px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
    </form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;

namespace IssueReporter
{
  /// <summary>
  /// Summary description for IssueReporter.
  /// </summary>
  public class IssueReporter : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox txtComment;
    protected System.Web.UI.WebControls.TextBox txtSender;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.CheckBox chkPriority;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.Button cmdSend;
    protected System.Web.UI.WebControls.Label lblResult;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdSend_Click(object sender, System.EventArgs e)
    {
      MailMessage msg = new MailMessage();
      msg.Subject = "Issue Report";
      msg.Body = "Submitted By: " + txtName.Text + "\n";
      msg.Body += txtComment.Text;
      msg.From = txtSender.Text;

      msg.To = "yourname@youremailserver.com";

      if (chkPriority.Checked) msg.Priority = MailPriority.High;

      SmtpMail.SmtpServer = "localhost";
      SmtpMail.Send(msg);

      lblResult.Text="Message sent to SMTP service.";
    }
  }
}

--%>

Send out an Email

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">

    void Page_Load()
    {
        SmtpClient client = new SmtpClient();
        client.Host = "localhost";
        client.Port = 25;
        client.Send("s@your.com", "b@mine.com", "Hi!", "Message."); 
    }

</script>
<html>
<head id="Head1" runat="server">
    <title>Send Mail</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    Email sent!
        
    </div>
    </form>
</body>
</html>

Send email with attached file

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMessage as String
    Dim TheMailMessage as New MailMessage
    Dim TheAttachment as MailAttachment
    TheMessage = txtName.Text _
        & ": Attached to this email is the " _
        & "file you requested."
    TheMailMessage.From = "us@a.com"
    TheMailMessage.To = txtEmailAddress.Text
    TheMailMessage.Subject = "File Request"
    TheMailMessage.Body = TheMessage
    TheAttachment = New MailAttachment( _
        Server.MapPath(ddlFile.SelectedItem.Value))
    TheMailMessage.Attachments.Add(TheAttachment)
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(TheMailMessage)

    

    lblMessage1.Text = "The file requested has been sent " _
        & "to the email address you entered.<BR>Enter Your Name"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>File Request Sample Site</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label
    id="lblMessage1"
    runat="Server"
    Text="Enter Your Name"
/>
<BR>
<asp:TextBox
    id="txtName"
    runat="server"
    MaxLength=50
/>
<BR><BR>
<asp:Label
    id="lblMessage2"
    runat="Server"
    Text="And Your Email Address"
/>
<BR>
<asp:TextBox
    id="txtEmailAddress"
    runat="server"
    MaxLength=50
/>
<BR><BR>
<asp:Label
    id="lblMessage3"
    runat="Server"
    Text="Select the file you wish to download"
/>
<BR>
<asp:DropDownList
    id="ddlFile"
    runat="server"
>
    <asp:ListItem 
        Value="Catalog.txt"
        Text="Catalog"
    />
    <asp:ListItem 
        Value="StoreLocations.txt"
        Text="Store Locations"
    />
</asp:DropDownList>
<BR><BR>
<asp:button 
    id="butOK"
    text="Send File"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
<BR>
</Font>
</Form>
</BODY>
</HTML>

Send email out through asp:form

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    TheMailMessage.From = txtFromEmail.Text
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending a Simple Email Message in Code</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the to email address:
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Enter the text of your message:
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Send email to all employees in database

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
    Dim I as Long
    TheMailMessage.From = txtFromEmail.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    DBConn = New OleDbConnection( _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "DATA SOURCE=" _
        & Server.MapPath("EmployeeDatabase.mdb;"))
    DBCommand = New OleDbDataAdapter _
        ("Select FirstName " _
        & "From Employee", DBConn)
    For I = 0 to DSPageData.Tables("FirstName"). _
        Rows.Count - 1
        TheMailMessage.To = DSPageData.Tables("FirstName"). _
            Rows(I).Item("FirstName") & "@hotmail.com"
        TheMailConnection.Send(TheMailMessage)
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending Email Blast</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<BR>
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Enter the text of your message:
<BR>
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Send email with html form message

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim TheMessage as String
    TheMessage = "<HTML><BODY>" _
        & "<FORM method=""post"" action=""" _
        & "http://yourwebsite/processrequest.aspx"">" _
        & "<B>Product Name: </B>ASP.net Book<BR>" _
        & "<B>Description: </B>These books are very nice!<BR>" _
        & "<B>Price: </B>$48.96<BR>" _
        & "<B>Quantity: </B><input name=""txtQuantity"" " _
        & "type=""text""/><BR>" _
        & "<input type=""submit"" value=""Order""/>" _
        & "<BR></FORM></BODY></HTML>"    
    TheMailMessage.From = "me@mycomapny.com"
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = "HTML Email"
    TheMailMessage.Body = TheMessage
    TheMailMessage.BodyFormat = MailFormat.Html
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Adding a Form to an Email Message</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
<BR>
<asp:button 
    id="butOK"
    text="Send HTML Email"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Email message in a html format

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim TheMessage as String
    TheMessage = "<HTML><BODY>" _
        & "<B>Product Name: </B>Bookss<BR>" _
        & "<B>Description: </B>These books are very nice!<BR>" _
        & "<B>Price: </B>$48.96<BR><BR>" _
        & "</BODY></HTML>"    
    TheMailMessage.From = "me@mycomapny.com"
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = "HTML Email"
    TheMailMessage.Body = TheMessage
    TheMailMessage.BodyFormat = MailFormat.Html
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending an HTML Email Message</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
<BR>
<asp:button 
    id="butOK"
    text="Send HTML Email"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>


Email form with cc and bcc

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    TheMailMessage.From = txtFromEmail.Text
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.CC = txtCCEmail.Text
    TheMailMessage.BCC = txtBCCEmail.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending an Email to Other Recipients</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the to email address:
<BR>
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
Enter the CC email addresses:
<BR>
<asp:textbox
    id="txtCCEmail"
    runat="server"
/>
<BR>
<BR>
Enter the BCC email addresses:
<BR>
<asp:textbox
    id="txtBCCEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<BR>
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Enter the text of your message:
<BR>
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Email with priority

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    TheMailMessage.From = txtFromEmail.Text
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    If ddlPriority.SelectedItem.Text = "High" Then
        TheMailMessage.Priority = MailPriority.High
    ElseIf ddlPriority.SelectedItem.Text = "Normal" Then
        TheMailMessage.Priority = MailPriority.Normal
    Else
        TheMailMessage.Priority = MailPriority.Low
    End If
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Setting the Priority of an Email Message</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the to email address:
<BR>
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<BR>
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Select the priority of the email message:
<BR>
<asp:dropdownlist
    id="ddlPriority" 
    runat="server"
>
    <asp:listitem>High</asp:listitem>
    <asp:listitem>Normal</asp:listitem>
    <asp:listitem>Low</asp:listitem>
</asp:dropdownlist>
<BR>
Enter the text of your message:
<BR>
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Send email function

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailConnection as SmtpMail
    TheMailConnection.Send(txtFromEmail.Text, txtToEmail.Text, _
        txtSubject.Text, txtMessage.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Using the Send Method Directly</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<asp:textbox
    id="txtFromEmail"
    runat="server"
/>
<BR>
Enter the to email address:
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
Enter the subject of your message:
<asp:textbox
    id="txtSubject"
    runat="server"
/>
<BR>
Enter the text of your message:
<asp:textbox
    id="txtMessage"
    runat="server"
    textmode="MultiLine"
    rows="5"
/>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Email with pictures

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)

End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim TheMessage as String
    TheMessage = "<HTML><BODY BACKGROUND=""bg.gif"" " _
        & "TEXT=""DarkRed"">" _
        & "<B>Product Name: </B>Books<BR>" _
        & "<B>Description: </B>These books are good!<BR>" _
        & "<B>Price: </B>$12.77<BR><BR>" _
        & "<img src=""books.gif"" border=2>" _
        & "</BODY></HTML>"
    TheMailMessage.From = "me@mycomapny.com"
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = "HTML Email"
    TheMailMessage.Body = TheMessage
    TheMailMessage.BodyFormat = MailFormat.Html
    TheMailMessage.URLContentLocation = _
        "http://yoururl/"
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending an HTML Email Message</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address:
<BR>
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
<BR>
<asp:button 
    id="butOK"
    text="Send HTML Email"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Send email with more than one attachments

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TheMailMessage as New MailMessage
    Dim TheMailConnection as SmtpMail
    Dim TheAttachment as MailAttachment
    Dim TheItem as ListItem
    TheMailMessage.From = "me@mycompany.com"
    TheMailMessage.To = txtToEmail.Text
    TheMailMessage.Subject = "File Request"
    TheMailMessage.Body = "Attached is the information " _
        & "you requested."
    For Each TheItem in lbFiles.Items
        If TheItem.Selected = True Then
            TheAttachment = New MailAttachment( _
                Server.MapPath(TheItem.Value))
            TheMailMessage.Attachments.Add(TheAttachment)
        End If
    Next
    TheMailConnection.Send(TheMailMessage)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Sending Multiple Files in an Email Message</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter your email address to recieve our Product Catalog:
<asp:textbox
    id="txtToEmail"
    runat="server"
/>
<BR>
Select all the files you wish to receive:
<asp:listbox
    id="lbFiles" 
    runat="server"
    selectionmode="multiple"
    rows=3
>
    <asp:listitem value="file.txt">Catalog</asp:listitem>
    <asp:listitem value="file.txt">Locations</asp:listitem>
    <asp:listitem value="file.txt">Privacy</asp:listitem>
    <asp:listitem value="file.txt">Jobs</asp:listitem>
</asp:listbox>
<BR>
<asp:button 
    id="butOK"
    text="Send"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>

Send out an email in case of page error

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Mail" %>
<html>
<head>
   <title>Throw an Error</title>
   <script runat="server">
      Sub Page_Load()
         Dim NullText As String = Nothing
         Message.Text = NullText.ToString()
      End Sub
      Sub Page_Error(Source As Object, E As EventArgs)
         Dim ex As Exception = Server.GetLastError()
         If Not ex Is Nothing Then
            Dim Mail as New MailMessage()
            'Change the values below to valid email addresses
            Mail.To = "yourEmailAddress@yourServer.com"
            Mail.From = "fromEmail@fromEmail.com"
            Mail.Subject = "error"
            Mail.Body = "An Exception occurred in page " & _
               Request.RawUrl & ":" & vbCrLf
            Mail.Body &= ex.ToString() & vbCrlf & vbCrlf
            Mail.Body &= "was handled from Page_Error."
            'If your SMTP server is not local, change the property below
            '   to a valid server or domain name for the SMTP server
            SmtpMail.SmtpServer = "localhost"
            SmtpMail.Send(Mail)
            Server.ClearError()
         End If
         Response.Write("An error has occurred. " & _
            "The site administrator has been notified.<br/>" & _
            "Please try your request again later.")
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>