Click here to Skip to main content
12,701,133 members (26,835 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

Embed image in Email - ASP.NET , C#

, 13 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Code to embed logo and other images while sending email


This is a simple article which tells you how you can embed images in emails. This can be very useful when you want to send emails from your website.A common scenario would be when a new user registers with your site and you want to send a welcome email with your site's logo on top.

I searched the net when I faced this issue. I am grateful to many authors from where I could make a start. I have used their code while writing this. Due credit goes to them all. The intention is to make the code available for the others too. To use this all you have to so is copy the code and paste it in any button click event.

This may only work with ASP.NET 2.0 & C#. I have tested it too. It will not run in ASP.NET 1.1.

Namespaces Used

using System.Net.Mail;
using System.Net.Mime;


// send mail to the new user who has registered.
  protected void yourButton_Click(object sender, EventArgs e)

           string strMailContent = "Welcome new user";
           string fromAddress = "";
           string toAddress = "";
           string contentId  = "image1";
           string path = Server.MapPath(@"images/Logo.jpg"); // my logo is placed in images folder
           MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
           mailMessage.Bcc.Add(""); // put your id here
           mailMessage.Subject = "Welcome new User";

           LinkedResource logo = new LinkedResource(path);
           logo.ContentId = "companylogo";
           // done HTML formatting in the next line to display my logo
           AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);

           mailMessage.IsBodyHtml = true;
           SmtpClient mailSender = new SmtpClient("localhost"); //use this if you are in the development server


Note: SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings["MyCustomId"]); // use this in the Production Server. I have specified my email server in the web.config file


The image may not be displayed in Outlook Express. It worked well when the mail was viewed in Yahoo. If your site is not a trusted one there is a chance that the image may be blocked. It did not work with Hotmail and Gmail.

There are other ways to show images, but like many authors have pointed out they do not embed the image. They always refer to the parent site. The image won't be displayed when offline or when the location of the image has changed in the parent site

Do please send your comments . I would be happy to hear from you.

Thank you , Happy Coding !


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


About the Author

Rajesh C Medackel
Web Developer
India India
A Mechanical Engineer, MBA working in Microsoft Technologies. Currently employed with a software major in Mumbai,India

You may also be interested in...

Comments and Discussions

QuestionOutlook Express, Hotmail, Windows Live mail Pin
shivam.mehta22-Nov-16 8:37
membershivam.mehta22-Nov-16 8:37 
QuestionOutlook Express, Hotmail, Windows Live mail Pin
shivam.mehta22-Nov-16 8:37
membershivam.mehta22-Nov-16 8:37 
SuggestionOutlook Express, Hotmail, Windows Live mail Pin
shivam.mehta22-Nov-16 8:37
membershivam.mehta22-Nov-16 8:37 
QuestionOutlook Express, Hotmail, Windows Live mail Pin
shivam.mehta22-Nov-16 8:38
membershivam.mehta22-Nov-16 8:38 
QuestionWhat if I am sending an html body that has a link image? Pin
Member 115255631-Apr-16 5:10
memberMember 115255631-Apr-16 5:10 
GeneralThanks Pin
Member 1024437210-Feb-15 23:22
memberMember 1024437210-Feb-15 23:22 
QuestionI have been able to embed images in Gmail Pin
electriac3-Jan-13 10:33
memberelectriac3-Jan-13 10:33 
Questionhtml templates in Pin
Hari Chowdary8-May-12 22:38
groupHari Chowdary8-May-12 22:38 
QuestionPicture not adding to the body of the mail Pin
Hayden Pinto9-Mar-12 6:51
memberHayden Pinto9-Mar-12 6:51 
GeneralMy vote of 2 Pin
Rajesh Duraisamy17-Aug-11 5:38
groupRajesh Duraisamy17-Aug-11 5:38 
GeneralChanges for it to work on Gmail and Thunderbird Pin
Member 786410722-Apr-11 2:32
memberMember 786410722-Apr-11 2:32 
GeneralMy vote of 1 Pin
Ram Shah23-Sep-10 0:50
memberRam Shah23-Sep-10 0:50 
QuestionHow to display images in Windows Live Mail [modified] Pin
Morcoveata23-Aug-10 3:12
memberMorcoveata23-Aug-10 3:12 
AnswerRe: How to display images in Windows Live Mail Pin
rurouniRonin3-May-11 10:12
memberrurouniRonin3-May-11 10:12 
AnswerRe: How to display images in Windows Live Mail [modified] Pin
shivam.mehta22-Nov-16 8:10
membershivam.mehta22-Nov-16 8:10 
Generalthanks ! Pin
aicha20089-Sep-09 1:56
memberaicha20089-Sep-09 1:56 
Questionhow to send mail within intranet. Pin
sudhir behera8-Sep-09 19:00
membersudhir behera8-Sep-09 19:00 
GeneralI had trouble getting this to work in Visual Studio 2008 Pin
mbizzile6-Feb-09 11:56
membermbizzile6-Feb-09 11:56 
Generaldo not work with outlook 2007 Pin
mrodrig@renacimiento.com24-Jul-08 2:06
membermrodrig@renacimiento.com24-Jul-08 2:06 
GeneralRe: do not work with outlook 2007 Pin
KenL_MCSE31-Oct-08 8:24
memberKenL_MCSE31-Oct-08 8:24 
GeneralRe: do not work with outlook 2007 Pin
Member 435949310-Dec-08 21:17
memberMember 435949310-Dec-08 21:17 
GeneralRe: do not work with outlook 2007 Pin
MaciusA28-Apr-10 23:17
memberMaciusA28-Apr-10 23:17 
GeneralRe: do not work with outlook 2007 Pin
shivam.mehta22-Nov-16 8:09
membershivam.mehta22-Nov-16 8:09 
GeneralAdd Button in Email Pin
dtkubal4-Mar-08 2:54
memberdtkubal4-Mar-08 2:54 
GeneralRe: Add Button in Email Pin
Rajesh C Medackel12-Mar-08 3:03
memberRajesh C Medackel12-Mar-08 3:03 
GeneralVery good article Pin
a-tibi27-Nov-07 3:44
membera-tibi27-Nov-07 3:44 
QuestionAdding Text after Image Pin
Sheila K26-Jul-07 10:08
memberSheila K26-Jul-07 10:08 
Questionimage going as attachement Pin
DilipV26-Jun-07 7:40
memberDilipV26-Jun-07 7:40 
AnswerRe: image going as attachement Pin
DilipV26-Jun-07 8:06
memberDilipV26-Jun-07 8:06 
Questionhow to send mail using outlook express 9.0 ( 2.0)? Pin
Jesu.Official14-Feb-07 18:08
memberJesu.Official14-Feb-07 18:08 
GeneralPost Code Pin
laokimsea5-Jan-07 20:24
memberlaokimsea5-Jan-07 20:24 
GeneralRe: Post Code Pin
Rajesh C Medackel29-Jan-07 4:01
memberRajesh C Medackel29-Jan-07 4:01 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 14 Dec 2007
Article Copyright 2006 by Rajesh C Medackel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid