C# / .NET Program Tutorial

C# Program to send email through STMP (with Authentication) –

Step to program to send Email through C#:

1. Initialize the required inputs:

In our program, we have provided Name, Email, Phone number, Subject and Message as the string datatype and assigned values to those strings.

2. Create the Object for SmtpClient

SmtpClient mailSender = new SmtpClient();

Assign the values to the mailSender object. Some of the required fields are stmp url, port, username and password.

3. Create the object for MailMessage

MailMessage mailMsg = new MailMessage();

4. Send your email!

Now, you can send you email through mailSender.Send(mailMsg);


Note: You need to import following Namespace.

1. using System.Net;
2. using System.Net.Mail;


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

namespace MailProgram
    class Program
        static void Main(string[] args)
            string name, email, mob, sub, msg;

            name = "aravind";
            email = "";
            mob = "9710";
            sub = "sub";
            msg = "this is the data";

            //creating object for StmpClient
            SmtpClient mailSender = new SmtpClient("", 587);
            mailSender.Credentials = new NetworkCredential("", "somepass");       
            mailSender.EnableSsl = true;

            //creating object for MailMessage
            MailMessage mailMsg = new MailMessage("", email, sub, msg);
            mailMsg.Priority = MailPriority.High;
            mailMsg.IsBodyHtml = true;
            mailMsg.SubjectEncoding = System.Text.Encoding.UTF8;

            //Sending Mail
                Console.WriteLine("Successfully Submitted!");


            //catching the exception
            catch (Exception err)



Leave a Reply

Your email address will not be published. Required fields are marked *