Thursday , February 21 2019
Home / Dot Net / C# / How to Send an Email In Perfect way Using C#

How to Send an Email In Perfect way Using C#

How to Send an Email In Perfect Way using C#:

Now a days for every website email communication is very important. for example if an user registered in your website then you need to send an automatic email to user for acknowledgment of his Registration of if he requests forgot password then you need to send an email for password reset instructions similarly in an eCommerce website when user placed any order then Commerce website needs to send an email to user for confirmation of his order.

When Sending Emails to User , Email should be very attractive design so always create Email Templates in the following Example i have created different email templates

As a C# programmer we need to write code for sending emails and  also some times we need to send Cc Emails and B CC Emails so Now in this Article I will Explain Perfect Coding Architecture for writing code to send email.

So i have taken basic Example of User Registration and Forgot Password.

Here when User Registered I need to send an Email to User and some Cc Mails and B CC Emails

Step 1:

I have created one database called Test and Create a table called Users in Sql Server.


Step 2:

I have Created one Empty Website(File>New>Website>Empty Website)

Step 3:

In Web.config Add Below Code

Change your Database setting in Connection string

in above web.config file you need to pass below values

  • SenderEmail Address in FromEmail Section
  • SenderEmail Password in FromPwd Section
  • PortNumner in PortNumber Section
  • Here i am using gamil so i send 587. gmail Uses 587 Port Number
    • Gmail-587 or 25
    • Yahoo-465 or 995
    • Hotmail- 25 or 465
    • if you want send from your own domain you need to pass 25 (25 is default for hosting Providers like godaddy,bigrock etc)
  • HostName in HostName Section
    • Here i am using gmail so i sended
    • if you want send from your own domain you need to pass ( is default for hosting Providers like godaddy,bigrock etc)
  • if your sending from https mail provoders like(from gmail,hotmail,yahoo) then you need to EnableSSL section to true otherwise set False
  • if you have CCEmail set those Email Addresses with comma in CCEmail Section like,,
  • similarly for BCC Emails if you have BCCEmail set those Email Addresses with comma in BCCEmail Section like,,

Step 4:

Add a class called EmailUtil and add below code in EMailUtil class

Step 5:

Create A Webpage Called Register.aspx and add below code



Here in SendRegEmail Method if you want to send emails to cc and bcc then set true for below variables otherwise set false

ob.IsSendCCEmail = true; //If you send CC Emails then Enable Here
ob.IsSendBccEmail = true; //If you want send Bcc EMails Then Enable Here


Step 6:

Add ForgotPassword.aspx and below code



Step 7:

create a folder with name MailTemplates under Root Folder and  create mail templates for both registration and forgot password


When user registred i will send welcome email to user for that purpose i am using above mail templates

Here we need to send pass some values like(name,email ) to register.html from our program so i have taken those values with @ like @username and i passing those values in Register.aspx.cs file SendRegMail Method like below


So our Final Structure would be like this

Email Soluction


Download Full Souce Code Here

Thanks for Reading this Article if you like this article please share and give your valuable comments

Print Friendly, PDF & Email

Give us your valuable feedback


About Murali

Hello everyone I am Software Developer having 3+ years of Experience in Microsoft Technologies and Others. My skills :-, C#,, SQL SERVER, web services, windows Service,Windows applications,HTML,CSS, JavaScript,JQuery,AnjularJs

Check Also

Studyclues_C#_Date Format

Date Formatting in C#

Date Formats in C#: Date Formatting in C# is a date and time format string …