Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use SMTP server to send email in ASP.NET

Author: Richard Wand

This article will explain what the SMTP server is and how you can use SMTP server to send email in ASP.NET

What is SMTP?

Simple Mail Transfer Protocol (SMTP) is the internet standard to send email from mail server to other mail server or from mail client to mail server. The SMTP is limited to queue messages at the receiving end that is why it is frequently used with POP3 or IMAP protocols to receive emails. When sending email with SMTP, you need to specify the SMTP server and port number in your email application configuration. Many email hosting providers provide SMTP server including Gmail, Yahoo, Hotmail, etc.


I am using Gmail SMTP server to send email but you can use whatever you like and provide host name and port number.
The System.Net.Mail namespace provide MailMessage class and SmtpClient class for sending email using SMTP in ASP.NET
  1. Create new website and write the following HTML code in aspx page of a web form

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <br />

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <br />

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  3. Write below button click event in code behind file of your web form.
    First declare an object of MailMessage class with “from” and “to” email address as string and set the “subject” and “body” properties of MailMessage object. You can also set other properties of MailMessage object like “cc” and “bcc”. Body of the email can also be set as HTML by setting IsBodyHtml property to true.  Now declare an empty object of SmtpClient and set its EnableSsl property to true. (Note: EnableSsl is only required when your SMTP Server must require it). At the end send email by using send method of SmtpClient object and pass MailMessage object as parameter value. 


    protected void Button1_Click(object sender, EventArgs e) {
         System.Net.Mail.MailMessage emailMessage = new System.Net.Mail.MailMessage("", TextBox1.Text);
         emailMessage.Subject = "Sample email";
         emailMessage.Body = "Sample email body";
         System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
         client.EnableSsl = true;
         Label1.Text = "Email Sent Successfully";


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim emailMessage As New System.Net.Mail.MailMessage("", TextBox1.Text) 
          emailMessage.Subject = "Sample email"
          emailMessage.Body = "Sample email body"
          Dim client As New System.Net.Mail.SmtpClient()
          client.EnableSsl = True
          Label1.Text = "Email Sent Successfully"
    End Sub
  4. The most important to send email using SMTP server is to configure SMTP host in web.config file or in code behind file. Write following code in web.config file just above the system.web tag.  Write your correct email address, user name and password to send email.

           <smtp from="">
               <network host="" port="587" userName="YourEmail" password="yourpassword

You can use other SMTP host instead of Gmail to send email but you have to write host name and port number according to that host. 


Add Article Comment:
Name :
Email Address :
Comments :
<< What are the ASP.NET Validation Controls and how to use them for client side validation

Disclaimer - Privacy
© 2002-2018