Click here to Skip to main content
6,822,613 members and growing! (16,069 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Embed image in Email - ASP.NET , C#

By Rajesh C Medackel

Code to embed logo and other images while sending email
Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:11 Dec 2006
Updated:13 Dec 2007
Views:60,807
Bookmarked:31 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.59 Rating: 2.92 out of 5
5 votes, 29.4%
1
1 vote, 5.9%
2
1 vote, 5.9%
3
3 votes, 17.6%
4
7 votes, 41.2%
5

Introduction

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;

 

CODE

 // send mail to the new user who has registered.
   protected void yourButton_Click(object sender, EventArgs e)
    {
            
            string strMailContent = "Welcome new user";
            string fromAddress = "yourname@yoursite.com";
            string toAddress = "newuser@hisdomain.com";
            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("inkrajesh@hotmail.com"); // 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);
            av1.LinkedResources.Add(logo);


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

 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

Remarks

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 !

License

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


Member
A Mechanical Engineer, MBA working in Microsoft Technologies. Currently employed with a software major in Mumbai,India
Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Generalthanks ! Pinmemberaicha20081:56 9 Sep '09  
Questionhow to send mail within intranet. Pinmembersudhir behera19:00 8 Sep '09  
GeneralI had trouble getting this to work in Visual Studio 2008 Pinmembermbizzile11:56 6 Feb '09  
QuestionImage stream attachments? PinmemberPSU Mike6:00 7 Aug '08  
Generaldo not work with outlook 2007 Pinmembermrodrig@renacimiento.com2:06 24 Jul '08  
GeneralRe: do not work with outlook 2007 PinmemberKenL_MCSE8:24 31 Oct '08  
GeneralRe: do not work with outlook 2007 PinmemberMember 435949321:17 10 Dec '08  
GeneralAdd Button in Email Pinmemberdtkubal2:54 4 Mar '08  
GeneralRe: Add Button in Email PinmemberRajesh C Medackel3:03 12 Mar '08  
GeneralVery good article Pinmembera-tibi3:44 27 Nov '07  
QuestionAdding Text after Image PinmemberSheila K10:08 26 Jul '07  
Questionimage going as attachement PinmemberDilipV7:40 26 Jun '07  
AnswerRe: image going as attachement PinmemberDilipV8:06 26 Jun '07  
Generalhow to send mail using outlook express 9.0 (asp.net 2.0)? PinmemberJesu.Official18:08 14 Feb '07  
GeneralPost Code Pinmemberlaokimsea20:24 5 Jan '07  
GeneralRe: Post Code PinmemberRajesh C Medackel4:01 29 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 13 Dec 2007
Editor:
Copyright 2006 by Rajesh C Medackel
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project