Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C#

Sending Email Using Embedded Images

Rate me:
Please Sign up or sign in to vote.
4.68/5 (18 votes)
25 Mar 2009CPOL 148.9K   47   10
How to send embedded images in email using .NET 2.0.

Introduction

Sending and receiving emails is a daily task for almost every professional, and programmers are not different. Sending emails and attachment is considered a trivial task, and all programming platforms support this feature.

Background

You can send an email with images as external links, but most email clients block external links. There is another way around to send images as part of an email.

Using the code

The following code is self explanatory. Here, we go:

  1. Create a string that contains the HTML message to send.
  2. Create an AlternateView object for supporting the HTML.
  3. Create a LinkedResource object for the image to send.
  4. Add a LinkedResource object to the AlternateView object.
  5. Create a Mailmesasge object and set its To, From, and Subject properties.
  6. Add an AlternateView object to the MailMessage object.
  7. Create an SmtpClient object and send the MailMessage object.
C#
using System.Net.Mail;

string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
    (htmlBody, null, MediaTypeNames.Text.Html);

// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
pic1.ContentId = "Pic1";
avHtml.LinkedResources.Add(pic1);


// Add the alternate views instead of using MailMessage.Body
MailMessage m = new MailMessage();
m.AlternateViews.Add(avHtml);

// Address and send the message
m.From = new MailAddress("rizwan@dotnetplayer.com", "Rizwan Qureshi");
m.To.Add(new MailAddress("shayan@dotnetplayer.com", "Shayan Qureshi"));
m.Subject = "A picture using alternate views";
SmtpClient client = new SmtpClient("smtp.dotnetplayer.com");
client.Send(m);

Points of interest

Since embedded images are part of emails, they can considerably increase the size of emails and can cause an email client to treat them as spam.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Neuron Management Services
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionDispose Resources Pin
eferreyra16-Sep-16 3:54
eferreyra16-Sep-16 3:54 
QuestionA long shot after 3 years but... Pin
John.Burke66626-Mar-16 15:10
John.Burke66626-Mar-16 15:10 
AnswerRe: A long shot after 3 years but... Pin
Garth J Lancaster26-Mar-16 16:52
professionalGarth J Lancaster26-Mar-16 16:52 
QuestionThe results not sufficient Pin
danies818-Jul-13 0:16
danies818-Jul-13 0:16 
QuestionVery Useful Pin
Andy McNiece6-Jun-12 5:28
Andy McNiece6-Jun-12 5:28 
Questionnot work for thunderbird Pin
cyman_y8-Feb-12 22:14
cyman_y8-Feb-12 22:14 
the solution is not workable for thunderbird(version 9.0.1)
GeneralMy vote of 5 Pin
smnabil14-Jul-10 21:54
smnabil14-Jul-10 21:54 
GeneralMy vote of 3 Pin
Simon_Whale7-Jul-10 1:02
Simon_Whale7-Jul-10 1:02 
GeneralIt works for attaching picture but not embedded them Pin
Hassaan Ahmed21-Nov-09 3:49
Hassaan Ahmed21-Nov-09 3:49 
GeneralNice. You may have a look at... Pin
axuno25-Mar-09 9:20
axuno25-Mar-09 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.