May 15, 2009

Sending email message having embedded image (.NET)

Filed under: .net,asp.net,programming Himanshu @ 5:16 am

Does your .net application send email messages? Does it send formatted email messages? I think applications should always send formatted email messages instead of text only. (Are asking why? Simple, formatted messages are much more capable, presentable, …).

Counter argument: Some user uses email client that are not capable to show HTML messages. Or someone is doing it for security purpose. Okay, I agree, then send both the views; text view as well as HTML view. Using .NET, one can send multi view email messages, depending on email client settings and/or capability, client is expected to pick the right view and shows that to the user.

Next question is, say we have decided to send email in html and text view, can we send images and style sheets embedded with email? Answers is yes. Here is how one can send multi-part email message using .net libraries that has embedded image.

Here goes the first step, create MailMessage object and setup that with appropriate values

   1: var from = new MailAddress("admin@objectpattern.com", "Himanshu");
   2: var to = new MailAddress("himanshu@objectpattern.net");
   3: var message = new MailMessage(from, to) { Subject = "Hi from ObjectPattern"};

next step changes. As we want to have two views – text and html, we will have to create email body differently.

in these lines of code, there are certain important points

   1: //creating text view
   2: var textViewContent = "Test content for text view";
   3: var textView = AlternateView.CreateAlternateViewFromString(textViewContent, Encoding.UTF8,
   4:                                                            MediaTypeNames.Text.Plain);
   5: //creating html view 
   6: var htmlViewContent = "

Html content containing image logo

Thanks,
ObjectPattern
logo";
   7: var htmlView = AlternateView.CreateAlternateViewFromString(htmlViewContent, Encoding.UTF8,
   8:                                                            MediaTypeNames.Text.Html);

that I would like to bring in to your notice.

  • Notice that we are not assigning Body property of MailMessage type
  • While creating view, we are specifying media type name as either plain text or html text, it can also be rich text.
  • We have created an img tag that referring to source as ‘cid:logo.png’

okay now how we will embed image into email message? That’s easy as well:

   1: var logo = new LinkedResource("images/logo.png", MediaTypeNames.Image.Jpeg) {ContentId = "logo.png"};
   2: htmlView.LinkedResources.Add(logo);

here in first line, first parameter of constructor (“image/logo.png” text) specifies the path of the image file. One can also supply image content as stream if its not coming directly from the file. Now remember, in above section, we had specified the src attribute of img tag, that should match with content id. Else image will not be shown correctly in email client. Also notice that we are adding this image to html view.

We are almost done now. All we need to do now is to add views to message object and sending using SMTP client.

   1: message.AlternateViews.Add(textView);
   2: message.AlternateViews.Add(htmlView);
   3:

6 Comments

  1. Thanks for taking the time to post this. I found it very useful – I had problems with the resource file name, though. If I used a relative path like “images/logo.jpg”, it would look for the file in the visual studio working directory (during debug). I ended storing the image’s full path in a configuration parameter and then it worked great.

    Comment by Chris Boothroyd — June 30, 2009 @ 5:13 am

  2. What is the HTML Tag to add a picture when we create webpage in Linux?

    Comment by internet marketing — July 19, 2009 @ 9:55 pm

  3. Thanks for all of the free code and the help with embedding images in email.

    Comment by Seo Services — August 2, 2009 @ 11:11 pm

  4. Hi,
    Traditionally, antivirus software heavily relied upon signatures to identify malware. This can be very effective, but cannot defend against malware unless samples have already been obtained and signatures created. Because of this, signature-based approaches are not effective against new, unknown viruses…..

    Comment by anti-spyware — August 13, 2009 @ 6:45 pm

  5. I found this article useful in a paper I am writing at university. Hopefully, I get an A+ now!

    Thanks

    Bernice Franklin

    Comment by UGG Boots — February 13, 2010 @ 10:46 pm

  6. I found it very useful !!!!

    Comment by Parantap — June 9, 2010 @ 7:46 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress