Sending HTML Mail in C#

In this post we would see how we can send a mail where the content is HTML in C#. To begin with, we need to use Microsoft.Office.Interop.Outlook assembly.


If you are having hard time finding the assembly, here is how you can find it. Select the Assemblies section and select Extensions. Scroll Down and find the assembly.


We would name the namespace; this would make code statements less lengthy.


The following SendMail method is where the logic is implemented. We would discuss the parts of the method.

In the following highlighted code, the Outlook.Application initialization and using the Namespace object, it tries to log in.


In the following highlighted code, a Outlook MailItem object is created as well as an Attachment object. The Attachment object could be sent as an attachment or could be embedded inside the html. The first parameter of the Attachments.Add method takes the full path of the file (image). To embed inside the image, you need to use the Outlook.OlAttachmentType.olEmbeddeditem type.


In the following highlighted code, the html image tag is created. Also a html string where the data is embedded with the html elements. And finally the MailItem type outlookMsg HTMLBody is assigned with the htmlString we just created.


In the following highlighted code, Recipients are added and using the Send method of MailItem object, the message is sent.


Note that, if your Outlook is not opened, it would run in background and you could see the icon in System tray.

The following shows the sent mail message as html.

Hope this post is useful.

Like Method Of Tried in Facebook.