ASP.NET Email application

SMTP, an acronym for Simple Mail Transfer Protocol, is a widely adopted communication protocol used for transmitting electronic mail (email) over the internet. The System.Net.Mail namespace encompasses a collection of classes specifically designed for facilitating the sending of emails to an SMTP server for eventual delivery.

By utilizing the classes within the System.Net.Mail namespace, developers gain the ability to construct and dispatch email messages to the designated SMTP server. This namespace provides a comprehensive set of functionalities, enabling seamless integration with SMTP servers and offering greater control over the email delivery process.

SMTP communication

Typically, SMTP communication occurs over port 25, which serves as the default port for SMTP operations. However, it's important to note that the specific port number may vary across different mail servers, allowing for flexibility and customization based on server configurations and requirements.


When working with email transmission in C#, two key classes come into play: the MailMessage class and the SmtpClient class. The MailMessage class serves as a representation of the content and attributes of an email message. It allows developers to define the sender, recipient(s), subject, body, attachments, and other relevant details essential for constructing a complete email message.

SmtpClient class

On the other hand, the SmtpClient class acts as the facilitator responsible for transmitting the email message to the designated SMTP host, which ultimately takes charge of delivering the email to its intended recipients. This class provides the necessary functionality for establishing a connection with the SMTP server, authenticating credentials if required, and transmitting the email in a secure and efficient manner.

The following ASP.NET source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is and the port using send mail is 587 and also using NetworkCredential for password based authentication.

SmtpServer.Port = 587 SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password") SmtpServer.EnableSsl = True
SmtpClient SmtpServer = new SmtpClient(""); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

The above code you have to replace username and password with you gmail user name and password. Also you have to provide the from address as your gmail address.

mail.From = New MailAddress("")
mail.From = new MailAddress("");

The following ASP.NET program using a gmail address as from address and server as gmail smtp server for sending a SMTP email message.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="To : "></asp:Label>             <asp:TextBox ID="to_txt" runat="server" Width="200px"></asp:TextBox> <br /><br /> <asp:Label ID="Label2" runat="server" Text="Subject :"></asp:Label>     <asp:TextBox ID="subject_txt" runat="server" Width="200px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label3" runat="server" Text="Message :"></asp:Label> <br /> <asp:TextBox ID="message_txt" runat="server" Height="117px" TextMode="MultiLine" Width="279px"></asp:TextBox> <br /> <br />                                     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send Mail" /> <br /> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(""); mail.From = new MailAddress(""); //you have to provide your gmail address as from address mail.To.Add(to_txt.Text ); mail.Subject = subject_txt.Text ; mail.Body = message_txt.Text ; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("gamil-username", "gmail-password"); //you have to provide you gamil username and password SmtpServer.EnableSsl = true; SmtpServer.Send(mail); Label4.Text = "Email successfully sent."; } catch (Exception ex) { Label4.Text = "Email Failed." + ex.Message; } } }
Full Source | VB.NET
Imports System.Net.Mail Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim mail As New MailMessage() Dim SmtpServer As New SmtpClient("") mail.From = New MailAddress("") 'you have to provide your gmail address as from address mail.To.Add(to_txt.Text) mail.Subject = subject_txt.Text mail.Body = message_txt.Text SmtpServer.Port = 587 SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password") 'you have to provide you gamil username and password SmtpServer.EnableSsl = True SmtpServer.Send(mail) Label4.Text = "Email successfully sent." Catch ex As Exception Label4.Text = "Email Failed." + ex.Message End Try End Sub End Class


Through the use of email class functions basing on their APIs, developers are able to instantiate emailing features into the software application by just centering on the process of sending and receiving messages that are possible through these facilities. Being a complete and fully supported way of creating email, sending it and delivering through the SMTP protocol, this course will present rules of the basis of any emailing process, and will let one to be sure about the communication and zero - message losing.