Exm is built into Sitecore 9 Experience Platform, You no longer need to install it as a package.
Step2: Create the instance exm model as below
Step3: Add the custom token like (body content for the email) and prepare email content
Step4: Create the contact and Add recipient
Step5: Create the contract and Add recipient
Step6: Replace Custom Tokens.
Step7: Attachment: Sitecore media item.
Step8: Sending email
We can specify target language when sending automated messages through the
client API.
To
send email programmatically through EXM, follow the below steps.
Step1: Create Exm model with necessary properties as show below.
public class ExmModel
{
public string MessageTemplateId { get; set;
}
public Dictionary<string, object>
CustomTokens { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public MediaItem Attachment { get; set;
}
public string FilePath { get; set; }
public string FileStream { get; set; }
public string FileName { get; set;
}
public string MediaItemID { get; set; }
}
//
Send Mail.
ExmModel exmModel = new ExmModel();
exmModel.MessageTemplateId = “email message
template id”
exmModel.MediaItemID =”Sitecore
MediaItemID”;
exmModel.CustomTokens
= new Dictionary<string, object>();
exmModel.CustomTokens.Add(token,
“heading”);
exmModel.CustomTokens.Add(token,
“Address”);
exmModel.CustomTokens.Add(token
name, “any content you wish to add”);
Create the
contact, List Manager in sitecore.
Please refer
the sitecore exm blog post to create the List manager and contacts
MessageItem message = Sitecore.Modules.EmailCampaign.Factory.GetMessage(exmModel.MessageTemplateId);
List<RecipientId> recipients = new List<RecipientId>();
Var
contact = contactRepository.LoadContactReadOnly(userName);
Var
lockAttempt = contactManager.TryLoadContact(contact.ContactId);
GetOrCreateContact(userName,
lockAttempt, contactRepository, contactManager, name);
}
recipients.Add(RecipientRepository.GetDefaultInstance().ResolveRecipientId("xdb:" +
contact.ContactId));
Step6: Replace Custom Tokens.
a.
Store
the attachment as media item in the sitecore
b.
Retrieve
the media item from sitecore
exmModel.Attachment = "get
the media item from sitecore"
message.Attachments.Add(exmModel.Attachment);
message.FromAddress = exmModel.Email;
if (recipients != null && recipients.Any())
{
foreach (var recipient in recipients)
{
New AsyncSendingManager(message).SendStandardMessage(recipient);
}
}
2. Piece of code to store uploaded file as a media item in sitecore
private static MediaItem AddingFile(ExmModel exmModel)
{
byte[] fileStreamDecoded = System.Convert.FromBase64String(exmModel.FileStream);
using (MemoryStream ms = new MemoryStream(fileStreamDecoded))
{
StreamWriter writer = new StreamWriter(ms);
writer.Write(fileStreamDecoded);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
//
Create the options
Sitecore.Resources.Media.MediaCreatorOptions options
= new
Sitecore.Resources.Media.MediaCreatorOptions();
options.FileBased = false;
options.IncludeExtensionInItemName = false;
options.Versioned = false;
options.Database =
ContextDatabase;
options.Destination =”exm file
path”
Sitecore.Resources.Media.MediaCreator creator
= new
Sitecore.Resources.Media.MediaCreator();
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// Item mediaItem =
Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream()
Item mediaItem =
creator.CreateFromStream(ms, exmModel.FilePath, options);
mediaItem.Editing.BeginEdit();
mediaItem.Name = Path.GetFileNameWithoutExtension(exmModel.FileName);
mediaItem.Editing.EndEdit();
return mediaItem;
}
}
}
3. Reference
No comments:
Post a Comment