Send Email Using ASP.Net With C#


There are many more articles on Google for sending an email using ASP.NET and C#. This article explains some important functions and notable errors.


    using System.IO;  
    using System.Net;  
    using System.Net.Mail;  
    string to = ""; //To address    
    string from = ""; //From address    
    MailMessage message = new MailMessage(from, to);  
    string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";  
    message.Subject = "Sending Email Using Asp.Net & C#";  
    message.Body = mailbody;  
    message.BodyEncoding = Encoding.UTF8;  
    message.IsBodyHtml = true;  
    SmtpClient client = new SmtpClient("", 587); //Gmail smtp    
    System.Net.NetworkCredential basicCredential1 = new  
    System.Net.NetworkCredential("yourmail id", "Password");  
    client.EnableSsl = true;  
    client.UseDefaultCredentials = false;  
    client.Credentials = basicCredential1;  
    catch (Exception ex)   
        throw ex;  

We must add the following namespace:

using System.Net;    
using System.Net.Mail;  

Simple Mail Transfer Protocol (SMTP)

Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP.

The following is a list of SMTP Server and Port Numbers:

Sl.No Mail Server SMTP Server( Host ) Port Number
1 Gmail 587
2 Outlook 587
3 Yahoo Mail 465
4 Yahoo Mail Plus 465
5 Hotmail 465
6 587
7 zoho Mail 465

Important SMTP Class Properties:

  • Host: Server URL for SMTP (check the preceding table).
  • EnableSsl: Check your host accepts SSL Connections (True or False).
  • Port: Port Number of the SMTP server (check the preceding table).
  • Credentials: Valid login credentials for the SMTP server (the email address and password).
  • UseDefaultCredentials: When we set to True in UseDefaultCredentials then that specifies to allow authentication based on the credentials of the account used to send emails.

Common Error

The following error will occur commonly for everyone. How to solve this? Check my previous blog contents.


We learned how to send emails using ASP.NET and C#. I hope this article is useful for all .NET beginners.

2 responses

  1. You are very cool!. Its useful and helpful information for testers…Keep Sharing.
    Thanks , just learn in send mail.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: