Thursday , December 14 2017
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 smtp.gmail.com
    • Gmail-smtp.gmail.com
    • Yahoo-smtp.mail.yahoo.com
    • hotmail-smtp.live.com
    • if you want send from your own domain you need to pass mail.domain.com (mail.domain.com 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 ccaddress1@gmail.com,cc2address@hotmail.com,cc3address@yahoo.com
  • similarly for BCC Emails if you have BCCEmail set those Email Addresses with comma in BCCEmail Section like bccaddress1@gmail.com,bcc2address@hotmail.com,bcc3address@yahoo.com

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

Register.Aspx

Register.aspx.cs

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

<!–nextpage–>

Step 6:

Add ForgotPassword.aspx and below code

ForgotPassword.aspx

ForgotPassword.aspx.cs

Step 7:

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

UserRegistration.html

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

ForgotPassword.html

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

comments

About Murali

Hello everyone I am Software Developer having 3+ years of Experience in Microsoft Technologies and Others.

My skills :- Asp.net, C#,Ado.net, SQL SERVER, web services, windows Service,Windows applications,HTML,CSS,
JavaScript,JQuery,AnjularJs

Check Also

Static Class

C# Static Class

C# Static Class: A C# static class cannot be instantiated. In other words, you cannot …

%d bloggers like this: