Send Email using SMTP Server in ASP.NET

Send Email using SMTP Server

This application creates an ASP.NET website that allows the end users to send e-mail messages. The application uses G-mail as an SMTP mail server for sending e-mails to recipients. The step involved in the creation of the email application in ASP.NET are:

1. Identifying an SMTP mail server

2. Creating the website

3. Designing the website (.aspx)

4. Adding code to the C# file (.aspx.cs)

5. Running the application

Identifying an SMTP mail server

To send e-mails from your website, you need to identify an SMTP mail server that could receive and process the e-mail sending requests. You can either choose from any of the SMTP service providers or use one of the free SMTP servers available on the web such as G-mail.

To use G-mail as an SMTP mail server, use the following settings in your web application:

SMTP Server Address: smtp.gmail.com
Port: 587 or 465
Enabled: True

Creating the website

To create the ASP.NET website, you need to perform the following steps:
1. Open Microsoft Visual Studio and select File -> New -> Website to display the New Website dialogue box.

2. Select the ASP.NET Empty website option under templates -> Visual C# section and enter the name of the website next to the Web location field.

3. Click OK to create the website. A new project gets added to the solution explorer.

4. Right Click the new project node in the solution explorer and select Add -> Add New Item to display the Add New Item

5. Select the Web Form option and click the Add button to open the Default.aspx page in Visual Studio.

Designing the website

To design the main page of the New Web site, you need to switch to the Design View of the Default aspx page and add appropriate controls corresponding to the To, Subject and Message fields of the e-mail message.

Adding code to the C# file

To add the e-mail functionality to the Default.aspx page, you need to add the relevant code in the corresponding C# file. Open the Default.aspx.cs file from the solution explorer and add the following code to it:

Running the application

Press F5 to start building the website, the Default.aspx webpage.

Now enter the e-mail address of the recipient and the mail body in the respective text field and click the send button to send the e-mail. A confirmation message indicates whether the message has been successfully sent or not.