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:
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.
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??
From email id is your(sender) id and To email id is receiver id.
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.
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..
Yes u r right… I am also getting the same error… “The remote name could not be resolved: ‘Host IP of SMTP'”
The remote name could not be resolved: ‘Host IP of SMTP’ error pls help mee
Convert your VB programs to C# with over 99% accuracy with the VBConversions VB.Net to C# Converter, the most accurate VB.Net to C# Converter available. Free download available at http://www.vbconversions.com
vb.net to c#
Very informative, Thanks for sharing. Here is a very useful code samples for Sending Email Messages using Exchange Server. I am sure it will be useful for the community as well. Here is the code sample: http://www.aspose.com/docs/display/emailnet/Send+Email+Messages+using+Exchange+Server
David Zondray, Thank you very much for you comments.