The Hoju Saram

Tuesday, November 07, 2006

Enhanced SMTPClient Class

I have been working on two areas of e-mail lately with .net. These are:

  • Sending invitations from .net so that outlook with except them properly.
  • Sending in-line MIME images
Both of these things require manipulation of the Content-Type headers which the standard system.net.mail.smtpclient does not support so I have written my own class to handle these two scenarios. The invitations are sent using a vcalendar class.

The class is CorpSMTPMailer (download here) and has two public methods of the class are

public void Sendicalinvite(string frommail, string[] tomail, DateTime EventStart, DateTime EventEnd, string Summary, string Location, string Organizer, string Description, string URL, bool Async)

public void SendMimeInLineMessage(string frommail, string[] tomail, string subject,string HTMLBody,string[] InLineAttachFiles,string[] InLineAttHTMLTokens,bool Async)

Both methods have an Async parameter, this determines where the functions are run in the current thread or a new thread is created to actually do the mail delivery. I haven’t included any feedback mechanism for error trapping as I don’t require it so if you need some them you will need to add it yourself.

The only thing to note is that the SendMimeInLineMessage function requires the HTMLBody to contain the tokens that are specified in the InLineAttTMLTokens Array so it knows where to place the content ids of the files specified in the InLineAttachFiles Array.

So for instance your code might look something like this. ( Please note I have changed the greater then and less than signs to [ ] in this code snippet as the blogger editor refused to render them properly )

List[string] MesgAttach = new List[string]();
List[string] tokens = new List[string]();

string body = "[div style=\"font-family:Arial\"]First picture attachment:[br /][br /][img src=\"@@IMAGE1@@\"][br /][br /]Second picture[br /][br /][img src=\"@@IMAGE2@@\"][br /][br /][/div]";

MesgAttach.Add(@"C:\pics\pic1.jpg");
MesgAttach.Add(@"C:\pics\pic2.jpg");
tokens.Add("@@IMAGE1@@");
tokens.Add("@@IMAGE2@@");

Labels: , ,

0 Comments:

Post a Comment

<< Home