Class MessagingHandler
- Namespace
- Dynamicweb.Mailing
- Assembly
- Dynamicweb.Core.dll
Messaging handler used for sending messages to recipients in EmailMessaging.
public class MessagingHandler
- Inheritance
-
MessagingHandler
- Inherited Members
Examples
using System;
namespace Dynamicweb.Mailing.Examples
{
class MessageRecipientSendingSample
{
public void SendMessage()
{
//Create a recipient collection and fill it with 100 recipients.
var recipientCollection = new RecipientCollection();
for (var i = 0; i < 100; i++)
{
var recipient = new Recipient();
recipient.Name = "Test" + i;
recipient.EmailAddress = string.Format("test{0}@testdomain.tld", i);
recipientCollection.Add(recipient);
}
//Create a message that will be sent.
var message = new Message();
message.Subject = "This is a test email";
message.HtmlBody = string.Format("<h1>Hello <!--@{0}--></h1><br /><p>This is a test</p>", Constants.TagRecipientName);
message.SenderEmail = "webshop@mydomain.tld";
message.SenderName = "Test Webshop";
//Instanciate the MessagingHandler, which will start the process.
//Using the CallbackHandler is reviewed in another example.
var handler = new MessagingHandler(message, recipientCollection);
var processStarted = handler.Process();
//The boolean 'processStarted' indicated whether the process of preprocessing, merging and sending was started.
//This process is run asynchronously in multiple threads to increase performance.
if (!processStarted)
throw new Exception("Sending could not be started");
}
}
}
Remarks
Uses Dynamicweb.Context.Current for later thread execution
Constructors
MessagingHandler(Message)
Creates a new instance of the messaging handler. This constructor should only be used when a send process is resumed.
public MessagingHandler(Message message)
Parameters
Examples
using System;
namespace Dynamicweb.Mailing.Examples
{
class ResumeMessageSample
{
public void ResumeMessage()
{
//Assume them message with id 20 has already been sent,
//but the send process failed somewhere through the process.
//This code will resume the send process for the given message.
//Get the message with id 20.
var message = Message.GetMessageById(20);
//Create a new message handler
var handler = new MessagingHandler(message);
//Resume the message.
var processResumed = handler.ResumeProcess();
if (!processResumed)
throw new Exception("Unable to resume send process");
}
}
}
MessagingHandler(Message, RecipientCollection?)
Creates a new instance of the messaging handler. This constructor should only be used when starting a new send process, not when resuming.
public MessagingHandler(Message message, RecipientCollection? recipients)
Parameters
message
MessageThe Message to handle.
recipients
RecipientCollectionThe recipients to handle.
Examples
using System;
namespace Dynamicweb.Mailing.Examples
{
class MessageRecipientSendingSample
{
public void SendMessage()
{
//Create a recipient collection and fill it with 100 recipients.
var recipientCollection = new RecipientCollection();
for (var i = 0; i < 100; i++)
{
var recipient = new Recipient();
recipient.Name = "Test" + i;
recipient.EmailAddress = string.Format("test{0}@testdomain.tld", i);
recipientCollection.Add(recipient);
}
//Create a message that will be sent.
var message = new Message();
message.Subject = "This is a test email";
message.HtmlBody = string.Format("<h1>Hello <!--@{0}--></h1><br /><p>This is a test</p>", Constants.TagRecipientName);
message.SenderEmail = "webshop@mydomain.tld";
message.SenderName = "Test Webshop";
//Instanciate the MessagingHandler, which will start the process.
//Using the CallbackHandler is reviewed in another example.
var handler = new MessagingHandler(message, recipientCollection);
var processStarted = handler.Process();
//The boolean 'processStarted' indicated whether the process of preprocessing, merging and sending was started.
//This process is run asynchronously in multiple threads to increase performance.
if (!processStarted)
throw new Exception("Sending could not be started");
}
}
}
MessagingHandler(Message, RecipientCollection?, CallbackHandler?)
Creates a new instance of the messaging handler.
This constructor can be used wither when starting a new send process or when resuming.
When resuming, the recipients argument can be null
.
public MessagingHandler(Message message, RecipientCollection? recipients, CallbackHandler? callbackHandler)
Parameters
message
MessageThe Message to handle.
recipients
RecipientCollectionThe recipients to handle.
callbackHandler
CallbackHandlerThe callback handler to use in this context.
Examples
using System;
using System.Net.Mail;
namespace Dynamicweb.Mailing.Examples
{
class UsingCallbackHandlerSample
{
public void SendWithCallbackHandler()
{
//Create a recipient collection and add recipient
var recipients = new RecipientCollection();
var recipient = new Recipient();
recipient.Name = "Test";
recipient.EmailAddress = "test12@testdomain.tld";
recipients.Add(recipient);
//Get a message with a specific id.
var message = Message.GetMessageById(20);
//Instanciate a new message handler.
//This will use the TestCallbackHandler, which will alter the process.
var handler = new MessagingHandler(message, recipients, new TestCallbackHandler());
//Start the send process.
var processStarted = handler.Process();
if (!processStarted)
throw new Exception("Unable to start process");
}
}
class TestCallbackHandler : CallbackHandler
{
//This is one of the methods you can override in the CallbackHandler class.
public override void OnBeforeDelivery(CallbackHandlerContext context, Message message, Recipient recipient, MessageDeliverer deliverer, System.Net.Mail.MailMessage mailMessage)
{
//Imagine you want to change the way a message is delivered for a specific message and recipient.
//Do the check
if (message.Id == 20 && recipient.EmailAddress == "test12@testdomain.tld")
{
//Set the new delivery provider
deliverer.DeliveryProvider = new TestDeliveryProvider();
}
}
}
class TestDeliveryProvider : MessageDeliveryProvider
{
public override bool Deliver(MailMessage mailMessage)
{
//This is just a simple implementation using the Dynamicweb EmailHandler system.
//This could also be an external system that can send an System.Net.Mail.MailMessage.
return EmailHandler.Send(mailMessage);
}
}
}
Methods
Preview()
Saves the Message to the database, if possible. If save is successful, the Message and RecipientCollection are processed and merged to create a list of MailMessage object to be used for Preview. This method does not start the process in a separate thread.
public Dictionary<int, MailMessage> Preview()
Returns
- Dictionary<int, MailMessage>
A list of ready-to-use MailMessage objects.
Process()
Saves the Message to the database, if possible. If save is successful, the processing, merging and delivery of the Message and RecipientCollection is started in a separate thread.
public bool Process()
Returns
- bool
A indicating whether the process was started successfully.
ProgressActive(int)
public static bool ProgressActive(int messageId)
Parameters
messageId
int
Returns
ResumeProcess()
Resumes the process. The RecipientCollection is fetched based on the unsent recipients in the database. This method ignores the RecipientCollection that was passed to the constructor, if one exists.
public bool ResumeProcess()