views:

1221

answers:

1

I know how to send mails using the Microsoft Enterprise Library 2.0 using a text formatter. But these emails are always in plain text. Is there any way with entlib 2.0 to send these mails in HTML format?

+3  A: 

Well that is funny, I am now writing my own answer.

What I did was use the source code of entlib.

Within Microsoft.Practices.EnterpriseLibrary.Logging and Microsoft.Practices.EnterpriseLibrary.Logging.TraceListenerData

I found the classes that I needed.

Copy EmailMessage.cs to EmailMessageHTML.cs Copy EmailTraceListener.cs to EmailHTMLTraceListener.cs Copy EmailHTMLTraceListenerData

Put these classes in your own new Library Project.

Within EmailMessageHTML change all constructors to match the new classname and than ADD following line to the method:

    protected MailMessage CreateMailMessage()
            {
              .....

                message.IsBodyHtml = true;
              .....

                return message;
            }

After that, I had to use this new EmailMessageHTML class in EmailHTMLTraceListener (change EmailMessage to EmailMessageHTML) and also use this EmailHTMLTraceListener in the new EmailHTMLTraceListenerData.cs file.

Compile this new project and than use this in your config as follows (example)

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
 <listeners>
  <add toAddress="[email protected]" fromAddress="[email protected]" subjectLineStarter="" subjectLineEnder="My HTMLemailLogger" smtpServer="localhost" smtpPort="25" formatter="Text Formatter" listenerDataType="MYLibrary.HTMLEmailLogger.EmailHTMLTraceListenerData, MYLibrary.HTMLEmailLogger, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="None" type="MYLibrary.HTMLEmailLogger.EmailHTMLTraceListener, MYLibrary.HTMLEmailLogger, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="EmailHTML TraceListener"/>
 </listeners>

and add a valid category to log this to of course:

<add switchValue="All" name="OutOfBalanceBooking">
       <listeners>
        <add name="Database Trace Listener"/>
        <add name="EmailHTML TraceListener"/>
       </listeners>
      </add>

Of course you need some HTML document to than log with EntLib. I leave that as an exercise for the reader. And indeed! I get a nice HTML email now for every outofbalance booking that customers make on the site...

Hace