Though spammers have been abusing e-mail
technology for the past couple of years, e-mail continues to be the
de facto standard for business communications. Therefore, Web site
visitors still expect e-mail to be an option when/if they want to
contact the company. The functionality is simple to add when
working with ASP.NET sites–as long as the Web server is properly

Server setup

While .NET makes it easy to work with e-mail
messages, the process often fails due to improper server setup.
Internet Information Services
(IIS) uses the standard e-mail
Simple Mail Transfer Protocol
(SMTP). You can work closely with
the IIS administrator to ensure proper functionality, but the
following scenarios may exist:

  • Local test environment: If you’re developing an application on your
    local machine, you can configure IIS to utilize your e-mail
    application to send mail messages via IIS’s relay setting. This is
    setup via the Relay button on the Access tab in the SMTP server’s
    properties. You should add the local machine’s IP address
    ( to the list of users allowed to relay through the
  • SMTP server within an organization: If an SMTP server is properly
    set up on IIS, you can easily send e-mail with applications hosted
    on that server.
  • SMTP server that uses another server to send e-mail: While a SMTP
    server may be set up on IIS within an organization, you may use
    another SMTP service to process mail messages. (This may be due to
    firewall or IP blocking scenarios.) This is set up via the Advanced
    button on the Delivery tab in the SMTP server’s properties. The
    address of the server to process the messages is added to the Smart
    Host field. This makes the server forward all mails to that address
    for processing.

With the server configured, the application may
be coded to send e-mails where necessary.

The namespace

The .NET Framework’s System.Web.Mail namespace
provides everything necessary to work with mail messages. It
contains two classes for working with e-mail: MailMessage and

  1. MailMessage: This class has methods and properties for working with
    mail messages.
  2. SmtpMail: This class allows you to work with the IIS’s SMTP Virtual
    Mail services.

The various aspects of a mail message are
familiar; this includes the recipient, sender, subject, body, and
so forth. The MailMessage class contains numerous properties for
working these items, including Attachments, Bcc, and Subject (which
is usually the first line of filtering).

These properties may be set on a MailMessage
object, and the message is sent on its way via the SmtpMail object.
The SmtpMail class accomplishes this using one
property–SmtpServer–and one method–Send().

  • SmtpServer is the e-mail server used to send the message.
  • Send() is the method used to send a message. A MailMessage object
    may be passed to the method as a parameter. Another signature
    allows an e-mail to be sent without a MailMessage object.

Now let’s put everything together with example

Messages in motion

This simple example allows the user to send an
e-mail message from a Web page by specifying the To, Subject, and
Body fields and clicking a button. Two buttons are included, with
the first sending the message and the second clearing the fields.
Here is the page’s HTML:

<%@ Page language=”c#”
Codebehind=”Email.aspx.cs” AutoEventWireup=”false”
Inherits=”BuilderExamples.Email” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”
<title> – Sending email via
<meta name=”GENERATOR” Content=”Microsoft Visual Studio .NET
<meta name=”CODE_LANGUAGE” Content=”C#”>
<meta name=vs_defaultClientScript
<meta name=vs_targetSchema
<body MS_POSITIONING=”GridLayout”>
<form id=”frmSendEmail” method=”post” runat=”server”>
<asp:Label id=lblTo style=”Z-INDEX: 101; LEFT: 32px; POSITION:
absolute; TOP:
72px” runat=”server” Width=”144px”
<asp:TextBox id=txtTo style=”Z-INDEX: 102; LEFT: 176px;
POSITION: absolute;
TOP: 72px” runat=”server”
<asp:Label id=lblSubject style=”Z-INDEX: 103; LEFT: 32px;
POSITION: absolute;
TOP: 96px” runat=”server” Width=”136px”
<asp:TextBox id=txtSubject style=”Z-INDEX: 104; LEFT: 176px;
absolute; TOP: 96px” runat=”server”
<asp:Label id=lblBody style=”Z-INDEX: 105; LEFT: 32px;
POSITION: absolute; TOP:
120px” runat=”server” Width=”144px”
<asp:TextBox id=txtBody style=”Z-INDEX: 106; LEFT: 176px;
POSITION: absolute;
TOP: 120px” runat=”server” Width=”368px”
<asp:Button id=btnSend style=”Z-INDEX: 107; LEFT: 184px;
POSITION: absolute;
TOP: 248px” runat=”server” Width=”136px” Text=”Send” Font-
<asp:Button id=btnClear style=”Z-INDEX: 108; LEFT: 344px;
POSITION: absolute;
TOP: 248px” runat=”server” Width=”136px” Text=”Clear” Font-

The ASP.NET HTML establishes the interface with
the following codebehind processing the user’s request (clicking a

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 BuilderExamples  {
public class Email : System.Web.UI.Page  {
protected Label lblTo;
protected TextBox txtTo;
protected Label lblSubject;
protected TextBox txtSubject;
protected Label lblBody;
protected TextBox txtBody;
protected Button btnSend;
protected Button btnClear;
private void Page_Load(object sender, EventArgs e) { }
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)  {
private void InitializeComponent()  {
this.btnSend.Click += new
this.btnClear.Click += new
this.Load += new System.EventHandler(this.Page_Load);
private void btnSend_Click(object sender, System.EventArgs
e)  {
MailMessage msg = new MailMessage();
msg.To = this.txtTo.Text;
msg.Subject = this.txtSubject.Text;
msg.Body = this.txtBody.Text;
msg.From = “”;
SmtpMail.SmtpServer = “localhost”;
private void btnClear_Click(object sender, System.EventArgs e)
this.txtSubject.Text = “”;
this.txtBody.Text = “”;
this.txtTo.Text = “”;
} } }

Notice the System.Web.Mail namespace is made
available to the code via a using statement. You could extend this
code to perform data validation on the fields or allowing the user
to specify other mail fields like the Cc fields.

Another approach

While using the .NET Framework to compose and
send e-mails is very powerful, you can easily provide e-mail
functionality with the HTML mailto URL as well. It works the same
way as a normal link, by creating a new mail message via the user’s
default mail program instead of going to a Web site. For example,
the following mailto link would send a message to

<a>Send an

Note: This method depends upon the user having
an e-mail application set up.

TechRepublic’s free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!