Skip Navigation Links



In Asp.net, how do I embed images in an email?

Author : Kalpesh Satasiya      Blog :Software Development - Developer | Web Designer Ahmedabad India      Date: 8/23/2012 1:28:00 PM



static void EmbedImages()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, 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:companylogo'> 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\\logo.gif" );
logo.ContentId = "companylogo";

//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);
}

Bloggers

active bloggers in the last 24 hrs. Number shown in the bracket represents number of posts published in past 24 hrs,


other authors(29)

Firoze Shakir Photographerno1(7)

Piyal Kundu(6)

Abrachan Pudussery(5)

Shobha(4)

Fidarose Isha(3)

sankarshan(3)

Anil(2)

Kalyan P(2)

mervin anto(2)

MUNZ TDT(2)

R.D. Bhalekar(2)

Richa Singh(2)

Sanjeev(2)

Santosh bs(2)

SM(2)

wanderingsoul(2)

A grain of sand(1)

aamjunta(1)

Admin(1)

Adwait Sharma(1)

Aishwarya(1)

Amarendra(1)

Aravind GJ(1)

Asim Kumar(1)

Being Pramoda...(1)

brocas area(1)

Cardamom(1)

celestialrays(1)

chandrashekhara(1)

Chetan(1)

Chethan S(1)

Chitz(1)

clarityy(1)

Cynthia Z(1)

Deeps(1)

Dialect Of Heart(1)

Disha(1)

Disha Mishra(1)

Govind Kumar(1)

Guha Rajan(1)

hAnGp(1)

Heartcrossings(1)

Indrani(1)

IS(1)

jaideep khanduja(1)

jaish_vats(1)

Kalpana Misra(1)

kanoisandeep(1)

Kartikeya Date(1)

Kratik(1)

Krishna Rao(1)

Krithi Karthi(1)

Kunal(1)

Little Fingers(1)

magiceye(1)

magiceye(1)

Mahendra(1)

Malathy Badri(1)

Management Punditz(1)

Meena(1)

Mitra - Meet Integrate Transform Achieve(1)

Muthukrishnan Rajaram(1)

Nanka(1)

Nik P(1)

P.S. Suresh Kumar(1)

pinksocks(1)

Poonam Sharma(1)

Pramod Ganapathi(1)

Pratik Mandrekar(1)

Preety(1)

Priya Srinivasan(1)

Raafay Awan(1)

Rajagopalan(1)

Ram N(1)

Rams(1)

Reema Sahay(1)

reetam(1)

Rishabh(1)

Robinsh Kumar(1)

Rohit Rai(1)

Rupertt Wind(1)

S(1)

Sadho Ram(1)

sangeeta(1)

Sauvik von Chakraverti(1)

scriptlarva(1)

Seetha(1)

Shailza Sood Dasgupta(1)

shaista dhanda(1)

Shilpa(1)

shohdah(1)

Shrutee Choudhary.(1)

Shveta(1)

Soupi(1)

Sriram Khé(1)

Srivalli(1)

Sudharsan Narayanan(1)

Sunil Jose(1)

Sunthosh Kumar(1)

Swapna(1)

Tarang Sinha(1)

Vandana Yadav(1)

Varun Nayak(1)

vblogics(1)

Vikram Karve(1)

Vinod Bidwaik(1)

Vishesh Shrivastava(1)

Vivek Juneja(1)

VJ Sharma(1)

wildflower(1)


garland of Languages of India
an amalgamation of the diversified traditions
gracefully presented with novelty
http://www.haaram.com