GoodStuff.Providers.MessagingProvider

This namespace contains tooling voor constructing and sending plain text and html messages using templates.

The MessagingService class provides the public interface to the service. The services uses a provider-based configuration for transportation of the message. The default implementation is the Smtp provider which will send the message by email. It is possible to implement your own provider to communicate with other backend systems like message queuing or SAP.

In code, you construct a list of parameters that define the dynamic values of the file, like so:

var msg = GoodStuff.Providers.MessagingService.CreateMessage();
msg.MessageClass = "testMessage";
msg["param1"] = "testvalue";
msg["recipient"] = "emailaddress@server.com";
GoodStuff.Providers.MessagingService.SendMessage(msg);

In your web.config or app.config, you will need to provide a template registration for this message class

 <sectionGroup name="goodStuff">
      <section name="messageService" type="GoodStuff.Providers.MessagingProviderSection, GoodStuff"/>
      <section name="smtpMessaging" type="GoodStuff.Providers.SmtpMessagingConfigurationSection"/>
    </sectionGroup>


<goodStuff>
<messageService defaultProvider="Smtp">
      <providers>
        <add name="Smtp" type="GoodStuff.Providers.SmtpMessagingProvider, GoodStuff"/>
      </providers>
    </messageService>
    <smtpMessaging forceRecipient="dummy@test.com">
      <messages>
        <add name="testMessage" subject="Test message {param1}" from="noreply@client.com" to="{recipient}" 
                 asHtml="true" templateFile="mailtemplates/test.htm" />
      </messages>
    </smtpMessaging>
</goodStuff>

This will read the test.htm file from the mailtemplates folder in the root of your application. The mail will be sent to 'emailaddress@server.com' and the subject will contain the value of param1. In the body of the message, you can use similar parameter expansion.

To use '{' or '}' in your message as a literal value, you will need to escape them according to the string.Format rules.

As you can see, the messageService itself takes very little parameters and is basically and empty shell. Most of the configuration (and template parsing) is done by the provider implementation.

SmtpMessaging config section takes an optional parameter forceRecipient that will redirect all messages (including CC and BCC) to the specified address, ideal for testing purposes.

By default, the provider will look for templates in the web application root. You can override this root folder with the templateFolder attribute on the StmpMessaging/messages section, to specify a root directory where the templates can be found.

inline images

If you are sending mail as Html, you can include images with the message. To do this, add a subfolder to the mail templates directory and place your images there. The template parser will detect all images with a relative path and embed them in the body of the message. You do not need to translate cid:/ style paths. Using this approach will allow you to see the resulting mail message in the designer of Visual Studio.

Example:

<p>
     <img src="images/logo.png"/>
</p>

Place the logo.png in the images folder below the location of the template and the logo will be embedded.

Tip

To stop your application from sending messages altogether, adjust the smtp settings in your web.config to have all messages delivered to a temporary folder:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\temp\pickup"/>
      </smtp>
    </mailSettings>
  </system.net>

Last edited Oct 7, 2011 at 10:32 AM by drblame, version 2

Comments

No comments yet.