March 24, 2025

Send E-mail using C Sharp

Today email is a part of our modern life. We can’t think a day without email. Many companies are providing email service. Most of them are free. But for that we need to follow their terms and conditions. With the help of .Net Framework and C# we can easily build our own email service. This article explains how to send e-mail in C#. Summary of the article:

  • Prerequisite for E-mail Sending
  • UI Design for E-mail Sending
  • C# Code to Send E-mail

Prerequisite for E-mail Sending
In order to transfer or receive data over the internet Microsoft .NET framework provides two namespaces. One is System.Net and another one is System.Net.Sockets. In C# language the SMTP protocol is used to send email. C# use  System.Net.Mail namespace for sending email.
We need a SMTP server also. If we don’t have any SMTP, we  can use free SMTP server. We can also use our gmail account. At first we need to configure this SMTP server.

UI Design for E-mail Sending
If Microsoft Visual Studio is installed, Start it and select New Project from the file menu.
Design a graphical interface for email sending. A sample UI is given bellow:
UI for email

C# Code to Send E-mail
Writhe the following C# code for email sending.

string Host = "Host IP of SMTP";
int Port = 25;

MailMessage mail = new MailMessage();

string to = txtMailTo.Text;
string from = txtMailFrom.Text;
string subject = txtMailSub.Text;
string body = txtMailBody.Text;
string mailCC = txtMailCC.Text;
string mailBCC = txtMailBCC.Text;

MailMessage message = new MailMessage(from, to, subject, body);
message.IsBodyHtml = true;

if (mailCC !="") message.CC.Add(mailCC);    // to add cc
if (mailBCC!="") message.Bcc.Add(mailBCC);  // to add bcc

// for attachment
if (txtAttachment.HasFile)
{
    string strFileName = System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);
    Attachment attachFile = new Attachment(txtAttachment.PostedFile.InputStream, strFileName);
    message.Attachments.Add(attachFile);
}

SmtpClient client = new SmtpClient(Host, Port);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);

Most of the enterprise applications have email facility to communicate with customers or clients. For that many developers uses different third party tools. But, third party tools are not always user friendly and they are difficult to customize. Some times they can arise security problems. With the help of this  tutorials anybody can easily develop his/her own email sending modules. And can integrate it with CRM system.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

9 thoughts on “Send E-mail using C Sharp

  1. I Tried your code it work fine .
    when i test it its sending email with any email id , Like if i send through xxxxx@xx.com it is sending me the mail with xxxxx@xx.com id . its not validating the email id in sender text box. It should take only the email id present in Exchange server list is there any way to fix this??

  2. When I configure this to send from my Google account. No matter what I put in the from field, the email sends from my google account. And the to recipient receives nothing unless the to is my google account.
    Please advise.

  3. when i tried your code am getting an error as….
    “The remote name could not be resolved: ‘Host IP of SMTP'”
    please help me to resolve this error..

  4. Yes u r right… I am also getting the same error… “The remote name could not be resolved: ‘Host IP of SMTP'”

Leave a Reply

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