Background information
The answer depends on your environment configuration. If you have a single server setup (i.e. the server is configured as both CD and CM) the solution is simple (read on), if the server roles are split on multiple servers e.g. one CD server, one CM etc. the solution is a bit more complicated.
That being said,
Sitecore.Modules.EmailCampaign.ClientApi()
is the right entry point for working (programmatically) with EXM. On a scaled environment this ensures that if you call e.g.
Sitecore.Modules.EmailCampaign.ClientApi
.SendStandardMessage(new Guid(MessageId), new XdbContactId(contact.ContactId), true);
from a CD server, the dispatch will be made from the CM server as the ClientAPI makes a request to the ecmclientservice.asmx (as defined in the connection string, see configuring EXM in a scaled environment). This is necessary as the CD role is not configured to dispatch, and in most cases you don't want it to be, as this can take up a lot of resources. Unfortunately the ClientAPI does not currently support custom tokens for automated messages.
Solution
Assuming you have created and activated the following automated (previously known as triggered) message (highlights added for tokens):
The method
Sitecore.Modules.EmailCampaign.Application.EmailDispatch.EmailDispatch.SendTriggered(Guid messageId, RecipientId recipientId, bool usePreferredLanguage = false, IDictionary<string, object> customPersonTokens = null)
supports exactly what you're trying to do, for example:
var customPersonTokens = new Dictionary<string, object>
{
{"meterreading", "your reading" }
};
SendTriggered(new Guid(MessageId), new XdbContactId(contact.ContactId), true, customPersonTokens);
This will apply both the default tokens (e.g. fullname and email) and the custom tokens, in that order.
This can be executed directly on a single server setup, but for the reasons stated above you will need to create an endpoint on the CM server that you can call from the CD server on a scaled setup. The CM server should execute the SendTriggered() message, and the CD server should send the contact id, message id and any custom tokens.
Side note about default tokens The
The default tokens are defined in
Sitecore.Modules.EmailCampaign.Core.Personalization.DefaultRecipientPropertyTokenMap
Sitecore.Modules.EmailCampaign.Core.Personalization.DefaultRecipientPropertyTokenMap()
You are unable to add your own facets to this, but you can replace it with your own implementation by changing the following element in Sitecore.EmailExperience.Core.config:
<recipientPropertyTokenMap type="Sitecore.Modules.EmailCampaign.Core.Personalization.DefaultRecipientPropertyTokenMap, Sitecore.EmailCampaign" singleInstance="true" />