Use HTML to format a System.Net.Mail email

Posted on 2009-05-01
Last Modified: 2013-12-25
Hello everyone,

I want to insert a banner link into a System.Net.Mail email message.

The image is located on a webserver so what I wanted to do was use the tag...

<img border="0" src="">

But when the mail gets sent via the .Net framework, the image isnt in the message, only the link

(And yes I set the body = html, although the body only contains the link above and the rest is text without any other html formatting)

We have constructed a program that periodically posts mail to users, but we want to make the messages look alot nicer than just straight plain text.
Question by:recruitit
    LVL 16

    Assisted Solution

    Images are only supported with recent versions of System.Net.Mail and you can't just insert the HTML code.

    Here are the details:

    ***QUOTED TEXT*** ( )
    Embedding images is something that is new with System.Net.Mail. To embed an image you will need to
    1. Create a LinkedResource object. The LinkedResource will actually contain the binary data of the Image. This binary data is encoded as part of the email, and sent along as part of the MailMessage. 
    2. Give the LinkedResource a unique name, also known as a Content-Id. 
    3. Create a HTML AlternateView. 
    4. Inside that HTML text, you need to use the standard <img> tag. 
    5. For the "src" value, you need to point it at the Content-Id of the LinkedResource image. This is done by using the syntax <img src="cid:whatever"> The "src=cid:" part is required for the email client to recognize the <img> tag as an embedded image, while the "whatever" part is the actual Content-Id of the LinkedResource image.  This will instruct the mail client to find an embedded image named "whatever" and display the contents *without* making a http:// request. 

    Open in new window


    Accepted Solution

    I just answered my own question.  Its very simple, you just need to format the whole thing as a web page.  If you insert this text into the System.Net.Mail.MailMessage Body and set it to html then it works perfectly, you can format the entire thing to look just like a web page.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <img src="someimagelink" width="980" height="70" border="1" alt="" title="" />

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now