News

Archives

Web Links

Syndication:

Embed image into email

If you are like me it bugs you that users are forced to download images you placed in a html based email. Don't fret, you can embed the image and the user's will not be prompted to download images... 
 
Highlights:
 
HeaderInfo - needed for Outlook 2007; Not sure about 03...
 
AlternateView - Using a linked resource, this class embeds the linked resource in our case the image,
 
public boolSendEmailWithImage(stringMailToStr, stringMailFromStr, stringSubjectStr, stringBodyStr, stringImgPath)
{

string HeaderInfo = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=us-ascii\">";  

 

try
{
MailMessagemessage = newMailMessage(MailFromStr, MailToStr);
message.Subject = SubjectStr;
LinkedResource lr = newLinkedResource(ImgPath);
lr.ContentId = "Image"
AlternateView av = AlternateView.CreateAlternateViewFromString(HeaderInfo + BodyStr, null, MediaTypeNames.Text.Html);
av.TransferEncoding = TransferEncoding.QuotedPrintable;
av.LinkedResources.Add(lr);
message.AlternateViews.Add(av);
message.IsBodyHtml = true;
SmtpClientclient = newSmtpClient(ConfigurationManager.AppSettings["MailServerSetting"].ToString(), 25); 
client.UseDefaultCredentials = true;
client.Send(message);
 
return true;
}
catch(Exceptionex)
{
return false;
}
}

Feedback

# re: Embed image into email

So it looks like this will load one image, correct? What would this look like if I had a rich/image-heavy HTML e-mail - is it as simple is looping through and instantiating LinkedResources? Just curious - may use this if that is the case :)

Cheers mate,
Sanjay 11/3/2008 7:23 PM | SanjayU

# re: Embed image into email

cant we just copy paste? 9/16/2009 5:48 PM | cep

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: