May 15, 2009

Sending email message having embedded image (.NET)

Filed under: .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("", "Himanshu");
   2: var to = new MailAddress("");
   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

   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);

Powered by WordPress