C# Code to send a Mail

Welcome to CodeSpread!

While working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects.

Lately, I saw a request in forum asking for code to send email in .Net. I was not able to answer his query properly as it was something that i wrote long back and because of copy pasting from old code, i had completely forgot how to do that. So Here i attempt to recollect my memories, I know its very simple and if some of you can help me add more value to this article,you are most welcome.

First thing first, we require a reference to the mail namespace in our cs file before we can start coding. we will include.

using System.Net.Mail;

This namespace will provide the necessary classes required to compose and send a mail. To compose a Mail, we have to create an instance of MaiMessage like

var mailMessage = new MailMessage();

There are members of this instance which are required to be assigned like ‘From’ Address, ‘To’ Address, subject, message body, any attachments. We have another class MailAddress which is used to create instance for ‘From’ Address and ‘To’ Address . I think if you see the code, then it would be much more clear.

mailMessage.From = new MailAddress("from");
mailMessage.To.Add(new MailAddress("to"));
mailMessage.Subject = "subject";
mailMessage.Body = "body";
if (!string.IsNullOrEmpty("fileName"))
mailMessage.Attachments.Add(new Attachment("fileName"));

Text written under quotes(“”) needs to be changes with your real values. Don’t miss that.

Now we have composed our message, only thing that remains now is to send it. Lets look, if we have some more class in our namespace to perform the operation. We certainly have a class SmtpClient to server our purpose.

var mailSmtpClient = new SmtpClient("SmtpHost", SmtpPort);
mailSmtpClient.Send(mailMessage);

We have created one instance of SmtpClient and passed the values of Smtphost which is the host-name of your mail server and the SmtpPort which has been configured to send a mail. Finally you just have to call their Send() method and pass your mail message. We have many overloads of all these methods available to facilitate the process but here, we have mentioned only the basic process. So, last step is to write all this code at one place.

var mailMessage = new MailMessage();
try
{
mailMessage.From = new MailAddress("from");
mailMessage.To.Add(new MailAddress("to"));
mailMessage.Subject = "subject";
mailMessage.Body = "body";
if (!string.IsNullOrEmpty("fileName"))
mailMessage.Attachments.Add(new Attachment("fileName"));

var mailSmtpClient = new SmtpClient("SmtpHost", SmtpPort);
mailSmtpClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}

That’s all from my side. Please provide your inputs.

Leave a Reply

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

  1. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?