Home » Documentation » Developing » Framework - Messaging
Litium.Framework.Messaging
MessengerBase
To send a certain type of mail (for example tip a friend mail), you need to implement a messenger. This is done by extending the MessengerBase class.
When extending it, the generic type TMessageArgs must be specified. This should be an object to pass when sending the e-mail. An example of this could be an Order object which contains the customers e-mail and other things needed. But it could also be a specially made object like the TipAFriendMessageArgs-object used below.
Two methods/properties need to be implemented. They are MessengerName which can be used for settings and other things and CreateMessage which creates the actual message.
The example below doesn't show it, but there's no problem in making the messenger as a singleton.
Example
public class TipAFriendMessenger : MessengerBase<TipAFriendMessageArgs>
{
protected override string MessengerName
{
get { return "TipAFriend"; }
}
protected override Message CreateMessage(TipAFriendMessageArgs messageArgs)
{
if (messageArgs == null) throw new ArgumentNullException("messageArgs");
string body = ...snip...;
string subject = ...snip...;
var message = new EmailMessage
{
Body = body,
ProviderName = "email",
FromName = messageArgs.MyName,
FromEmail = messageArgs.MyEmail,
Subject = subject,
Type = MessageType.Single
};
message.Recipients.Add(new EmailRecipient
{
Name = messageArgs.FriendName,
Address = messageArgs.FriendEmail
});
return message;
}
}
To call the send
Guid id = new TipAFriendMessenger().Send(new TipAFriendMessageArgs( ... ));
Provider
To send a certain kind of message (e-mail, sms, mms etc.), a provider needs to be implemented. This is done by implementing the interface IMessageProvider. Two methods need to be implemented. They are ValidateMessage which validates the message before sending and SendMessage which sends the message to the recipients passed to it. The provider locator uses the Castle Windsor IoC container to resolve the implementation for a provider. App/Web.config needs to specify the path to the IoC-config file:
<add key="Messaging.IoCConfigPath" value="..\..\..\Setting\Messaging.IoC.xml" />
Example
An example of an e-mail provider:
public class EmailProvider : IMessageProvider
{
public bool ValidateMessage(MessageTiny message, out string validationError)
{
if (string.IsNullOrEmpty(message.Body))
{
validationError = "Body is not set.";
return false;
}
... more validation ...
validationError = null;
return true;
}
public void SendMessage(MessageTiny message, Collection<recipient> recipients)
{
var from = new MailAddress(
message.CustomFields["FromEmail"],
message.CustomFields["FromName"]);
foreach (Recipient recipient in recipients)
{
SendMessageToRecipient(
message.CustomFields["Subject"],
message.Body,
from, recipient);
}
}
private static void SendMessageToRecipient(
string subject, string body, MailAddress from, Recipient recipient)
{
... send e-mail ...
}
}
Settings
There are different kinds of settings. These are implemented with Litium.Framework.Setting. To get these working you need to specify the path to the XML-files:
<add key="SettingXmlFilePathTemplate" value="..\..\..\Setting\{0}.xml" />
MessageType
Message types are a way of categorize e-mails and send e-mails in separate categories simultaneously. E.g. if a news letter with 100 000 recipients is sending, it should not block forgot password e-mails. The message types are: Single, SmallBulk and LargeBulk It's also possible to have different settings for different types. Sending To send the mails placed in the queue, the sender must be started. This is done by calling:
MessageSender.Start();
This will start one background thread for each MessageType. This can be called from a separate windows service or be called from Application_Start in Global.asax in a web project. With settings you can change the sleep time between sends and the number of messages that should be sent in each batch. This can be different for the different message types.
Clean-up
The clean-up handles batches and messages stuck in an invalid state and also remove old sent messages. The clean-up is started by calling:
MessageCleaner.Start();
DeleteEmptyBatches
Deletes all batches with an end date and that doesn't have any messages or recipients connected to it.
FinishOldUnfinishedBatches
Sets end date on old batches that are missing an end date.
Uses the setting: HoursBeforeFinishingBatch
DeleteOldCompletedBatches
Deletes old batches.
Uses the setting: DaysBeforeDeletingBatch
ResetStatusOnMessages
Resets the status to ReadyToSend on old messages with the status InProgress.
Uses the setting: HoursBeforeResettingStatusFromInProgress
Logging
Log4net is used for logging.