Click here to Skip to main content
6,292,426 members and growing! (11,335 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Email & SMTP     Advanced License: The Code Project Open License (CPOL)

Sending Mail With An Embedded Image

By Lavate Prashant B.

Sending Mail With An Embedded Image
C# 2.0, Windows, .NET, Visual Studio, Dev
Posted:20 Jun 2007
Views:14,720
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
11 votes for this article.
Popularity: 3.06 Rating: 2.94 out of 5
4 votes, 36.4%
1

2
1 vote, 9.1%
3
2 votes, 18.2%
4
4 votes, 36.4%
5

Introduction

This article will help to get a basic idea of mailing with an embedded image or object from ASP.NET 2.0 using SMTP client.

Background

When I was in need of the same code, I got a lot of code from different sites but in some cases the mail was going in particular cases only. I mean to say that in Yahoo, the image was displaying, but in others it was not. In some cases, the image was going as an attachment so I wrote this code which sends your image with the mail body. For example, if you want to send your company's logo in the body of the mail, you can do that with this code.

Using the Code

The code is very simple and pretty straightforward. As a prerequisite, you should have access to a mail server.

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("prashant@gmail.com");
mail.To.Add("you@gmail.com");

//set the content
mail.Subject = "Mail From Prashant Lavate";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString
    ("This is my text , viewable by those clients that don't support html", 
    null, "text/plain");

//then we create the HTML part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus <img src='cid:logo'> will map to a LinkedResource with a 
//ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString
    ("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\prashant.gif" );
logo.ContentId = "logo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);

//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail); 

Please rate the article and don't hesitate to write to me in case you have any doubts or concerns. Please do check out my other articles which may help you out.

History

  • 20th June, 2007: Initial post

License

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

About the Author

Lavate Prashant B.


Member
Prashant is working with Microsoft Technology from last two years. He has worked in ASP.NET,C#.NET,VB.NET,SQL Server.
Occupation: Web Developer
Location: India India

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Generalcode improvement Pinmemberkeeeeeeeeeeiff2:49 21 Sep '07  
GeneralRe: code improvement PinmemberRobcast22:04 14 Mar '08  
QuestionGmail replaces from address PinmemberMiguel Domingos2:21 20 Jul '07  
Generaldont work :( Pinmemberyarns0:47 17 Jul '07  
GeneralError in code Pinmemberjonmbutler5:56 20 Jun '07  
GeneralNice and clean PinmemberTom Janssens5:15 20 Jun '07  
GeneralRe: Nice and clean PinmemberPrashant Lavate0:40 2 Aug '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jun 2007
Editor: Deeksha Shenoy
Copyright 2007 by Lavate Prashant B.
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project