In this article I will explain with an example, how to send emails using the System.Net class in C# .Net.
 
 
Mail Server Settings in App.Config file
The following Mail Server settings need to be saved in the App.Config file.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to Gmail Username specified in credentials.
 
<system.net>
 <mailSettings>
    <smtp deliveryMethod="Network" from="sender@gmail.com">
      <network
        host="smtp.gmail.com"
        port="587"
        enableSsl="true"
        userName="sender@gmail.com"
        password="SenderGmailPassword"
        defaultCredentials="true" />
    </smtp>
 </mailSettings>
</system.net>
 
 
Namespaces
You will need to import the following namespaces.
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
 
 
Sending SMTP email using System.Net Class in C#
MailMessage class properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo Email address.
 
SmtpClient class properties
Following are the properties of the SmtpClient class.
Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password).
Port – Port Number of the SMTP server (Gmail: 587)
 
 
Sending Email using System.Net Class
When the Program is executed, the Recipient email address (To), Subject and Body is read as input from the User, while the Sender email address (from) values are fetched from the SMTP section of the App.Config file.
Then, all these values are set into an object of the MailMessage class.
An object of SmtpClient class is created and the settings of the SMTP server such as Host, EnableSsl, Username, Password, DefaultCredentials and Port are fetched from the SMTP section of the App.Config file.
Finally, the email is sent using the Send function of the SmtpClient class object and success message is displayed.
static void Main(string[] args)
{
    Console.WriteLine("Enter To Address:");
    string to = Console.ReadLine().Trim();
 
    Console.WriteLine("Enter Subject:");
    string subject = Console.ReadLine().Trim();
 
    Console.WriteLine("Enter Body:");
    string body = Console.ReadLine().Trim();
 
    SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
    using (MailMessage mm = new MailMessage(smtpSection.From, to))
    {
        mm.Subject = subject;
        mm.Body = body;
        mm.IsBodyHtml = false;
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Host = smtpSection.Network.Host;
            smtp.EnableSsl = smtpSection.Network.EnableSsl;
            NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
            smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
            smtp.Credentials = networkCred;
            smtp.Port = smtpSection.Network.Port;
            Console.WriteLine("Sending Email......");
            smtp.Send(mm);
            Console.WriteLine("Email Sent.");
            System.Threading.Thread.Sleep(3000);
            Environment.Exit(0);
        }
    }
}
 
 
Possible Errors
The following error occurs when you try to send email using Gmail credentials in your application.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
 
 
Solution
 
 
Screenshot
Send SMTP Emails using System.Net Class in C#
 
 
Downloads