Embedding your Company Logo in Emails sent with C#

Sending emails with C# is straightforward. You only need a few lines of code and a bit of configuration for the mail server as described in an earlier post. Taking the effort to send an email as a base line, you could jump to the conclusion that it shouldn’t be a problem to display your company logo inside your generated emails. But unfortunately it’s not that easy. That logo should be displayed in the content of the email and not just be added to the list of attachments. And embedded images are a totally different story.

What seems to be an easy task can take hours. However, if you are willing to take some dirty shortcuts you can implement that requirement in a few lines of code.

 

Export your Email Message

Requirements like the one with the company logo tend to come from people using Outlook. Outlook can do that without much effort and that’s the place where you should start. Create your message inside Outlook and add placeholders for all the dynamic parts you need to replace.

If you save this message as HTML, you will get an ugly and long file with embedded styles. Don’t try to clean that up. Email clients are way behind the current web standards and web viewers constantly ignore external styles. (The same is true for div-elements – use tables to format your blocks)

The big benefit of this approach: all the formatting you use is included and enriched with fallbacks for different Outlook versions.

 

Replace the Image Path

The image for your logo is stored inside the folder [NameOfSavedEmail]_files. That structure will not be reachable in the email. Search for every place your logo is referenced and replace it with this path:

The cid: is a contend-Id tag who needs to reference an embedded image of your email. You can use whatever name you like, as long as you use the same name in the HTML as you set in your code. I suggest you be specific about what it should point to. As soon as you need to add more images it will be hard to keep them apart.

Attention: There can be multiple references in your message to the same image. Replace the path in all images. Depending on your version of Outlook, it may be that the logo appears multiple times. Should this be the case you need to check which one you need and remove the others.

 

Creating the Message in C#

Images can only be displayed in HTML emails, therefore you need to set the IsBodyHtml property to true:

Don’t forget to replace the placeholders before you create the MailMessage. If you do it before, you can use a simple Replace() method of the message string.

The magic with embedded images happens in this method:

The image is embedded with a LinkedResource object, that takes the path to the logo in its constructor. The ContentId property is the name you give it to reference it in your HTML message for the src-attribute of the image tag (cid:…).

The AlternateView combines the HTML message and the image to the body of your email. And it even works in GMail:

 

Conclusion

Creating an email in C# with a company logo is a bit dirty. Use Outlook to help you with all the old versions and export your message from there. It’s not an ideal solution but so far the best I know of. Please leave a comment when you know a better way.

1 thought on “Embedding your Company Logo in Emails sent with C#”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.