The need to send out nicely html formatted email notification automatically by the system was raised as a requirement on one of my current projects. The last time I used C# to send out formatted emails with embedded images inside of it the email clients were still rather "spammers" challenged and all of images from within the html mail message caused the email app to automatically be retrieved from the server. The situation today is that most popular clients (outlook and gmail for example) block the images from being download by default. This leads to a rather nasty and not so pretty to look at email message (with all of the images missing).
I started googeling for a way to embed the images within the email message.
I have to say I didn't find a lot. Most of the links were pointing to some kind of a commercial .NET library which claims to support this kind of functionality, but I was more in the open source kind of mood. I continued searching and I stumbled across an image
attachment type of a solution
[http://www.systemwebmail.com/faq/3.3.aspx] (check out the first post down below..)
........... C# code
msg.BodyFormat = MailFormat.Html;
msg.Body = ""; //only name of image
filemsg.Body += "Any other stuff for body";
...MailAttachment imgObj = new MailAttachment(Server.MapPath("/") + "web/images/cmsm1_multi.gif"); //actual location of image
filemsg.Attachments.Add(imgObj);... //send the message
.......
That's easy to use but there is a downside to it that you actually see the images as attachments.
I guess that if you have less than five images is not that bad but if you have more it wouldn't give that professional kind of look.
The ultimate solution would be to use a great (and free) .NET library named DotNetOpenMail
[http://dotnetopenmail.sourceforge.net/faq.html]
It has all sorts of low level functionalists you wouldn't find in System.Web.Email
And it also does a great job of sending out html emails , with embedded images .
By using the DotNetOpenMail library I did the following:..
............C# Code
EmailMessage emailMessage = new EmailMessage();
emailMessage.FromAddress = new EmailAddress("spam_goes_here@gmail.com");
emailMessage.AddToAddress(new EmailAddress("spam_goes_here@gmail.com"));
emailMessage.Subject = "Just an example";
emailMessage.TextPart = new TextAttachment("This photo requires a better "+
" email reader.");
emailMessage.HtmlPart = new HtmlAttachment("<html><body>"+
"<p>Just a lame gif</p>"+
"<p><img src=\"cid:zroiy\" alt=\"Zysman Roiy\"/></p>");
FileInfo relatedFileInfo = new FileInfo(@"c:/temp/tmp.gif");
FileAttachment relatedFileAttachment = new FileAttachment(relatedFileInfo,
"zroiy");
relatedFileAttachment.ContentType = "image/jpeg";
emailMessage.AddRelatedAttachment(relatedFileAttachment);
emailMessage.Send(new SmtpServer("localhost"));
And this the result is :
The DotNetOpenMail is still in beta mode , but if you're using it just sending out styled emails as I do it does the job properly.
Have fun
Roiy