Write For Us

Sending Email with ASP.NET

In this article we will be discussing on how you can send emails using your ASP.NET applications.


Consider the following problem:

The new user registration Web application needs to automatically send an e-mail message confirming new user registration for an online shopping site. Since the application requires same subject and same body matter for the mail, we have then as strings already written in the code. Although if your application requires you to have text boxes where in you can write the subject and the body, you can very well do that.

Mechanism to Send an E-Mail Message from an ASP.NET page

In ASP.NET application, you can send electronic mails by using either the SmtpMail class or the MailMessage class found in the System.Web.Mail namespace. The SmtpMail class can be used to send simple emails that do not contain any special fields, such as cc field and the bcc field where as the MailMessage class can be used to send emails that contain special fields, such as cc field or the bcc field.

Syntax of SmtpMail class is as follows:

SmtpMail.Send(from, to, subject, message text)

To use the MailMessage class, you first need to create an instance of the class by using the following syntax:

MailMessage msg = new MailMessage();

To send an email message in HTML format, you can use the HtmlTextWriter class. To instantiate the HtmlTextWriter class, first create an object of the StringWriter class. This will represent a stream in which data is written as strings. The syntax for the same is given below:

StringWriter strwriter = new StringWriter();
HtmlTextWriter htmltxtwriter = new HtmlTextWriter(strwriter);

You need to send an email message confirming registration, although as per the application it was required that we Bcc it to the head of "E-Shoppe site", but for this demo sake we have made a text box, as alternate email id, to which we will be sending the email as Bcc. The user interface looks like this:

Note that for this to run you must include the following namespaces in your application:



Before you try this, you must check that the Microsoft SMTP Service is available. To check this, go to Run->type inetmgr. Select the "Default SMTP Virtual Server", and see the play button (shown in the shaded blue portion) is disabled. If it is not you should click the play button.

In case your SMTP Virtual Server is not configured, you need to select the "Default SMTP Virtual Server", click Actions in the menu bar, select properties, and go to Access tab. Click the Authentication button;

And check the Anonymous access as shown in the figure below:

You may need to change the SmtpServer, since its a web application:

Smtpmail.SmtpServer = "<IP address of your web server>";

Code to send the Email with ASP.NET

I am attaching the code for the same problem, you can download it. Notice the code in the click event of the button i.e. where the main functionality is being performed. Download example project.

Once you run this application, you would get emails on the respective email IDs. And the email would look like this:

Working with Attachments

You can also attach files in your emails. Perform the following steps to do this:

1. Drag from the HTML tab of the toolbox, a file field, it looks like as shown in the figure:

  You can also add this in the HTML:  <input id="myfile" type="file" runat="server">

2. Go to its properites window and change its ID to "myfile"

3. You need to have an object of MailAttachment to do this class, so make a declaration as

    public MailAttachment attach;

4. Now add the following code before you call the Send method of SmtpMail.

if(myfile.PostedFile != null)
  attach = new MailAttachment (@myfile.PostedFile.FileName);

This is only basic functionality, but you'll be fine if you don't have strong demands. Otherwise, you should get Auto Email which have a lot of features well tested on many other sites worldwide.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   

comments powered by Disqus