X
Tech

Sending e-mail via ASP.NET

Sending an e-mail is simple to add when working with ASP.NET sites, provided your Web server is properly configured.
Written by Tony Patton, Contributor

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.

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 protocol 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 utilise 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 (127.0.0.1) to the list of users allowed to relay through the server.
  • 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 SmtpMail.

  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 code.

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" >
<HTML><HEAD>
<title>Builder.com - Sending email via ASP.NET</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<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" Font-Bold="True">To:</asp:Label>
<asp:TextBox id=txtTo style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute;
TOP: 72px" runat="server" Width="360px"></asp:TextBox>
<asp:Label id=lblSubject style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute;
TOP: 96px" runat="server" Width="136px" Font-Bold="True">Subject:</asp:Label>
<asp:TextBox id=txtSubject style="Z-INDEX: 104; LEFT: 176px; POSITION:
absolute; TOP: 96px" runat="server" Width="360px"></asp:TextBox>
<asp:Label id=lblBody style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP:
120px" runat="server" Width="144px" Font-Bold="True">Body:</asp:Label>
<asp:TextBox id=txtBody style="Z-INDEX: 106; LEFT: 176px; POSITION: absolute;
TOP: 120px" runat="server" Width="368px" Height="112px"></asp:TextBox>
<asp:Button id=btnSend style="Z-INDEX: 107; LEFT: 184px; POSITION: absolute;
TOP: 248px" runat="server" Width="136px" Text="Send" Font-
Bold="True"></asp:Button>
<asp:Button id=btnClear style="Z-INDEX: 108; LEFT: 344px; POSITION: absolute;
TOP: 248px" runat="server" Width="136px" Text="Clear" Font-
Bold="True"></asp:Button>
</form></body></HTML>

The ASP.NET HTML establishes the interface with the following codebehind processing the user's request (clicking a button):

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)  {
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()  {
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
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 = "info@Builder.com";
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);
}
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 Builder.com:

<a href=mailto:editor@builderau.com.au>Send an email</a>

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

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

Editorial standards