Send Email with HTML Templates using C#

Durgesh Barwal
3 min readFeb 16, 2020

--

Hello All,

This blog consist of the information and code snippets of the complete tutorial for sending emails using C#. Here I captured the necessary information for complete learning for sending emails with attachments, with image source in the body, sending an HTML body, sending emails with Alternative views, basic error that new developer faces during writing code for email.

First of all, if you have hosting service, than that platform may provide you dedicated hosting services for sending emails.

If you want to send mail from your code base, without depending on the hosting service then, here is the right information for you.

You need two object one is the MailMessage (represent the email message that can be send using SmtpClient Class), and second SmtpClient (allow the application to send the email using SMTP Protocol).

So, Let first talk about “MailMessage” and it’s different properties which help us to compose our email message.

Here is the code snippet: -

namespaces :-

using System.Net.Mail;
using System.Net.Mime;

MailMessage Object :-

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(“Senders Email Address”);
mailMessage.To.Add(new MailAddress(“Receiver's Email Address”));
mailMessage.Subject = “Subject of the Mail”;

We can also add Cc and Bcc recipients email addresses using the below code

mailMessage.CC.Add(new MailAddress(“Cc Receiver's Mail Address”));
mailMessage.Bcc.Add(new MailAddress(“Bcc Receiver's Mail Address”));

Here is the code snippet for Email body, We can send the email body as a plain text or with HTML rich content. There is an property “IsBodyHtml”, we need to set true if there is an HTML body, otherwise false.

mailMessage.IsBodyHtml = true;
mailMessage.Body = "html template as a string for the body";

We can also send AlternateView (represent the format to view an Email message) instead of message body. Using AlternativeView, we can also link resource files such as image or any other resource in the HTML body easily. AlternateViews collection to specify views with other MIME types.

// Reading the Image for the path in MemoryStream 
MemoryStream ms = new MemoryStream(File.ReadAllBytes("image_path"));

// Create Linked Resource of the memory Stream of the Image
// Give the Content Id to the Linked Resource
LinkedResource Img = new LinkedResource(ms, MediaTypeNames.Image.Jpeg);
Img.ContentId = "MyImage";

// HTML Body with the Content Id of the Linked resource
string body = "<img src=cid:MyImage id=\"img\" alt=\"\" width=\"100px\" height=\"100px\" />";
//Create AlternateView from the HTML body and add resources object
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(Img);
// Attaching the Alternate View to the mailMessage object
mailMessage.AlternateViews.Add(alternateView);

You can also send the Attachments with the Email using below code:

// Attaching the Image File in the Email
MemoryStream ms1 = new MemoryStream(File.ReadAllBytes("file path"));
mailMessage.Attachments.Add(new Attachment(ms1, "filename.jpg"));
// Attaching the PDF File in the Email
MemoryStream ms2 = new MemoryStream(File.ReadAllBytes("file_path"));
mailMessage.Attachments.Add(new Attachment(ms2, "pdffile.pdf"));

This is all about the MailMessage object which contain all information of the Email.

Now, we have SmtpClient, to send prepared MailMessage . So Let’s start with SmtpClient, which allow sending email using SMTP Protocol.

Here, is the code snippet for the SmtpClient Object.

SmtpClient sMTPClient = new SmtpClient();
sMTPClient.EnableSsl = true;
sMTPClient.Host = "smtp.gmail.com";
sMTPClient.Port = 587;
sMTPClient.UseDefaultCredentials = false;
sMTPClient.Credentials = new System.Net.NetworkCredential("sender's email Address", "password");
sMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
sMTPClient.Send(mailMessage);

If you are using your gmail email_Id to send mail using SmtpClient object, your gmail account’s two-factor authentication should be deactivated. And we also need to disable google’s Less secure apps to allow SmtpClient to send mails.

That’s all. Hope you like you. Clap it. I will get you awesome clapping notifications.

--

--

Durgesh Barwal

Passionate about technology, Lifestyle, reading and Writing, and a SDE at Palnful