Geeks With Blogs
A Developer's Expedition www.kenl.net
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;
}
}
Posted on Sunday, November 2, 2008 8:52 PM | Back to top


Comments on this post: Embed image into email

# re: Embed image into email
Requesting Gravatar...
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
Left by SanjayU on Nov 03, 2008 7:23 PM

# re: Embed image into email
Requesting Gravatar...
cant we just copy paste?
Left by cep on Sep 16, 2009 5:48 PM

# re: Embed image into email
Requesting Gravatar...
simple way with automatic web tool
http://www.neuron-webagency.com/shop/eng/convert-html-to-eml
Left by pierluigi on Oct 12, 2010 8:57 AM

Your comment:
 (will show your gravatar)


Copyright © Ken Lovely, MCSE, MCDBA, MCTS | Powered by: GeeksWithBlogs.net