ASP.NET 3.5 – How to send an email using exchange server from the ASP.NET Application?

The .NET Framework 1.x had a System.Web.Mail class to send an email from the ASP.NET framework. While this namespace and these classes still exist in the .NET Framework version 2.0 and later, they have been deprecated and replaced by the new Mail API in the System.Net.Mail namespace in the ASP.NET 2.0 framework. ASP.NET 1.x’s System.Web.Mail API was based on CDO libraries. With the new APIs, Microsoft moved away from CDONTS based wrapper APIs and written the new API using COM+ components to improve the performance.

ASP.NET 2.0 sends an email using SmtpClient class. In the most basic configuration, You have to set the hostname of the relay server if you are using exchange server or localhost if you are using local SMTP service, port (25 by default), authentican credentials, or specified pickup directory through the DeliveryMethod property

Here is the template for the System.NET.Mail configuration.
<!– Add the email settings to the element –>
<smtp deliveryMethod=”PickupDirectory” from=”fromemailaddress”>

localhost – local web server SMTP service – If you want to send an email through local SMTP Service of the web server, simply add following lines of code in your web.config to send an email from the ASP.NET web pages.

<smtp deliveryMethod=”PickupDirectoryFromIis”>
<network host=”(localhost)” port=”25″ defaultCredentials=”true” />

Exchange Server – If you want to send an email from existing exchange server email account, you have to setup the relay service from your webserver to exchange server. Following web.config configuration allows you use relay service for exchange server.

Let’s say exchange server name is “”, exchange username and password is “exchangeuserid” and “exchangepassword”, you web.config settings would be,

<network host=”” port=”25″ userName=”exchangeuserid” password=”exchangepassword” defaultCredentials=”false” />

Next step would be to create a class to send an emails using SMTP Service: Note that following code uses Web.config settings. Commented out code won’t use web.config mail settings. Although its preferrable, if you don’t want email configurations in web.config file, use the commented out code to configure and send an email from the ASP.NET pages.

using System;using System.Net;using System.Net.Mail;
public class SMTPEmailSender
public SMTPEmailSender()
// TODO: Add constructor logic here

public static void SendSMTPEmail(string senderMailAddress,
string recipientMailAddress,
string mailSubject,
string mailBody)
//Create MailMessage to send an email.
MailMessage message = new MailMessage(senderMailAddress, recipientMailAddress);
message.Subject = mailSubject;
message.Body = mailBody;

//Use SMTPClient to send an email.
//Uses SMTP settings from web.config
SmtpClient client = new SmtpClient();

//Uses SMTP Settings from Code
//Sample Code
//SmtpClient smtp = new SmtpClient(“”, portnumber);
//smtp.Credentials = new NetworkCredential(“exchangeuserid”, “exchangepassword”, “DOMAIN”);
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //smtp.Send(message);

Create a Test Page to send out emails. Here is the source code from the code behind to send an email from the test email page.
public partial class TestEmail : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
string fromEmailAddress = ““;
string recipientEmailAddress = ““;
string mailSubject = “Nik’s Website: Test Email”;
string mailBody = “Test Email.”;
if (recipientEmailAddress != null && recipientEmailAddress.Trim().Length != 0)
SMTPEmailManager.SendSMTPEmail(fromEmailAddress, recipientEmailAddress, mailSubject, mailBody);
Response.Write(“Test Email Sent Out”);

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: 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