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.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("prashant@gmail.com");
mail.To.Add("you@gmail.com");
mail.Subject = "Mail From Prashant Lavate";
AlternateView plainView = AlternateView.CreateAlternateViewFromString
("This is my text , viewable by those clients that don't support html",
null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString
("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
LinkedResource logo = new LinkedResource( "c:\\temp\\prashant.gif" );
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient("127.0.0.1");
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
Prashant is working with Microsoft Technology from last two years. He has worked in ASP.NET,C#.NET,VB.NET,SQL Server.