Reliability of C# SmtpClient email code (when images are involved)

149 views Asked by At

I have, so far as I know, a piece of relatively vanilla SMTP Client code in my C# Dot Net 4.0 web application. This has been working fine for sometime now, cranking out text messages with minimal HTML formatting. (If memory serves, it was based on this: http://blog.jdconley.com/2009/01/fire-and-forget-email-webservices-and.html)

Now the powers that be want to add graphics. The details have yet to be finalized but for the moment I am assuming that they would want to use graphic images either by themselves or as background for some HTML construct.

Frankly I am not an email expert. After a bit of hacking I managed to get this to work, sort of. (This hack is based on this: http://www.intstrings.com/ramivemula/articles/how-to-send-an-email-using-c-net-with-complete-features/ I also added an AlternativeView list to the MessageInfo object for processing in the main mail routine. The logo image itself is a part of the project, sitting in the root.)

I have several worries here.
First and foremost, how generic is this? Is this the best approach? How many types of mail server will be able to receive and properly display this?

In my testing, this succeeds when I send it to myself in Outlook over the company server. This message does not arrive when I try to send it to my personal accounts at Yahoo or Gmail. (Yes, I've checked the spam buckets.)

Suggestions?

MessageInfo mi = new MessageInfo();
MessageStatusInfo msi = new MessageStatusInfo();
EmailAgent ea = new EmailAgent();         
ea.minfoDefaultCommunicationSettings = new DefaultCommunicationSettingsInfo();
mi.EmailToAddresses.Add("[email protected]");            
mi.EmailFrom = "[email protected]";
mi.EmailSubject = "Email Test";
mi.IsBodyHtml = true;
mi.SMTPServer = "My Company's Email Server";
string str = "<html><body><table border='5'><tr><td><img src=\"cid:image1\"></td></tr></table></body></html>";
AlternateView av = AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(Server.MapPath("logo.jpg"), MediaTypeNames.Image.Jpeg);
lr.ContentId = "image1";
av.LinkedResources.Add(lr);
mi.avc.Add(av);            
msi = ea.SendMail(mi);
0

There are 0 answers