The Sitecore Email Experience Manager is your way to send personalized emails to Sitecore users. You do not need to send bulk emails, you can easily send single emails with contents like “Here is your new password” or “Your profile has been updated”.
The emails to send are “Standard Messages” so the email you create must be of the type “Triggered message”:
There is 2 ways of sending emails: To Sitecore users or Sitecore Contacts.
SEND AN EMAIL TO A SITECORE USER
Your Sitecore user must exist in the Sitecore User repository (usually as an “extranet” user).
using Sitecore.Data; using Sitecore.Diagnostics; using Sitecore.Modules.EmailCampaign; using Sitecore.Modules.EmailCampaign.Messages; using Sitecore.Modules.EmailCampaign.Recipients; public void Send(ID messageItemId, string userName) { MessageItem message = Factory.GetMessage(messageItemId); Assert.IsNotNull(message, "Could not find message with ID " + messageItemId); RecipientId recipient = new SitecoreUserName(userName); Assert.IsNotNull(recipient, "Could not find recipient with username " + userName); new AsyncSendingManager(message).SendStandardMessage(recipient); }
You call the function like this:
Send(new ID("{12A6D766-CA92-4303-81D2-57C66F20AB12}"), "extranet\\user@domain.com");
SEND AND EMAIL TO A CONTACT
The contact must (obviously) contain an email address. To create a contact see Sitecore Contacts – Create and save contacts to and from xDB (MongoDB). The code is near identical to the previous, but the Receipient is retrieved by resolving the contact ID:
using Sitecore.Data; using Sitecore.Diagnostics; using Sitecore.Modules.EmailCampaign; using Sitecore.Modules.EmailCampaign.Messages; using Sitecore.Modules.EmailCampaign.Recipients; public void Send(ID messageItemId, Guid contactID) { MessageItem message = Factory.GetMessage(messageItemId); Assert.IsNotNull(message, "Could not find message with ID " + messageItemId); RecipientId recipient = RecipientRepository.GetDefaultInstance().ResolveRecipientId("xdb:" + contactID); Assert.IsNotNull(recipient, "Could not find recipient with ID " + contactID); new AsyncSendingManager(message).SendStandardMessage(recipient); }
You call the function like this:
Send(new ID("{12A6D766-CA92-4303-81D2-57C66F20AB12}"), Guid.Parse("c3b8329b-7930-405d-8852-7a88ef4f0cb1"));
MORE TO READ:
- EXM recipient profiles by Sitecore
- Recipient repositories by Sitecore
- Sitecore Contacts – Create and save contacts to and from xDB (MongoDB) by myself
Hi Brian,
Do you know the implications of calling AsyncSendingManager(message).SendStandardMessage(recipient) on the CD servers. Sending is supposed to occur on the CM, we have logic similar to above getting fired on the CDs after a form submission, which is throwing ‘The type initializer for ‘Sitecore.EDS.Core.Dispatch.DispatchManager’ threw an exception’, because the configuration doesn’t exist. SHould the SendStandardMessage call be going through the WebService to get processed on the CM server, or is there another call that would trigger this? Or do you need to configure our CDs to deliver, as well as the CM?
Kind regards,
Ryan
LikeLike
No, the SendStandardMessage() does not know about your CD/CM setup, and it will try to send the email from CD.
So you will have to build the dispatch yourself. I have a similar setup where I created a simple .ashx page on my CM server that the CD server calls every time they need to send an email.
You could also configure the CD server to allow it so send the email.
Remember, that if you are using a Contact as recipient, you will need to use the Sitecore Shared Session Manager as the CM server need to access the same contact as the CD server.
LikeLike
Hi, Brian
I follow your instruction but cannot found the SitecoreUserName class, could you tell me where I can get it?
Thanks and Regards,
Lavie
LikeLike