HomeASP HostingWeb DesignRegister DomainSupportAbout Us

Support :: FAQ

How to send email from ASP.NET page
One of the most common functionalities used in web development is sending email from a web page. Before you ask, Iíll give you a couple of reasons why you might want to send email from your web application:

  • create feedback web page
  • implement "forgot password" script that sends a password to the userís email
  • send welcome email to your new newsletter subscriber
  • send automatic email update notification
  • send automatic email notification whenever an error occur in your web application

    Of course there are many more situations where is appropriate to send email from your web application and it's up to you how and where exactly you are going to do it.

    The .NET framework makes the task of sending email from a web page unbelievably easy and I'll show you how to do it in a matter of minutes. This article presumes that you have a basic understanding of how .NET and ASP.NET works.

    We are going to use the SmtpMail and MailMessage classes, which belong to the System.Web.Mail namespace. In order to use them, we will need to import the System.Web.Mail namespace like this:

    <%@ Import Namespace="System.Web.Mail" %>

    The MailMessage class provides properties and methods for constructing an e-mail message. To build our email message we need to create an instance of this class:

    Dim objMail As New MailMessage() On the next step we have to set all the properties of the objMail object: ' The email address of the sender objMail.From = "yourname@yourdomain.com" ' The email address of the recipient objMail.To = "recipientname@somedomain.com" ' The email address of the Cc recipient objMail.Cc = "name1@anotherdomain.com" ' The email address of the Bcc recipient objMail.Bcc = "name2@anotherdomain.com" ' The format of the message - it can be MailFormat.Text or MailFormat.Html objMail.BodyFormat = MailFormat.Text ' The priority of the message - it can be MailPriority.High, ' MailPriority.Normal or MailPriority.Low objMail.Priority = MailPriority.High ' The subject of the message objMail.Subject = "My first ASP.NET email" ' The message text objMail.Body = "This is my first email sent via ASP.NET. "

    After all the properties of our new email message are set properly, the only thing left is to send the message. If you have experience sending email from classic ASP script with CDONTS you might think that the MailMessage class has method Send or something similar. Well the way an email message is sent is a little different in ASP.NET. We need to use the static method Send of the SmtpMail class, passing in our objMail instance. You might ask what does static member mean? This means that this method is not associated with an instance of a type. Example of an instance of a type is our objMail object. It is illegal to reference a static member of a class through an instance. The proper way to do it is:


    If you want to specify different SMTP server, than the default one you need to set the SmtpServer property of the SmtpMail class:

    SmtpMail.SmtpServer = "smtp.your-server.com"

    In summary to send an email from your ASP.NET page you need to:

  • import the System.Web.Mail namespace in your ASP.NET page
  • create an instance of the MailMessage class
  • set all the properties of the MailMessage instance
  • send the message with SmtpMail.Send method
  • © Copyright 2001-2004 Art Branch Inc.