-
Couldn't load subscription status.
- Fork 32
Message Error Handling
Norbert Bietsch edited this page Mar 7, 2018
·
1 revision
MailMergeLib is frequently used in unattended environments like task schedulers or batch jobs. This means, that properly dealing with message errors is a key requirement.
This is very simple:
// Event raising when getting the merged MimeMessage of the MailMergeMessage has failed.
var mms = new MailMergeSender();
mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) => { // do sth. useful }; Event arguments (MailMessageFailureEventArgs) let you observe and tackle failures during message generation:
- The
MailMergeMessagewhich was the argument to one of theSendmethods. - The data source which was involved during message generation. Another argument to the
Sendmethods. - An AggregateException with all reasons in detail why it was thrown, and a MimeMessage, as far as it could be generated.
- A
MimeMessageparameter which can be returned to the caller. - A
boolparameter to decide whether the sender should raise a MailMergeMessageException
Inner exceptions of exception parameter contains AddressException, AttachmentException, EmtpyContentException, MailMergeMessageException, VariableException, ParseException) with more detailed reasons.
When the OnMessageFailure event is fired, there are several ways to react. The most common ones are:
- Write to failure with any necessary additional information to log file
- Analyze the reasons for the failure, modify the
MailMergeMessageand return it to the sender. - Create a new instance of
MailMergeMessage, call aGetMimeMessagemethod and assign it to theMimeMessageparameter which the sender will then send. - Set the
bool throwExceptionto instruct the sender, whether aMailMergeMessageExceptionshall be thrown. Iftrueyou may implement more features in atry...catch...block. Note: TheMimeMessageparameter must benullin case offalse, otherwise an exception will throw anyway.
var mms = new MailMergeSender { Config = ... };
mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) =>
{
lock (_locker)
{
// Remove the cause of the exception and return corrected values
// Note: changes of MailMergeMessage will affect als messages to be sent
messageFailureArgs.MailMergeMessage.PlainText = plainText.Replace("{BadVariableName}", string.Empty);
// in production a try...catch... must be implemented
messageFailureArgs.MimeMessage = messageFailureArgs.MailMergeMessage.GetMimeMessage(messageFailureArgs.DataSource);
messageFailureArgs.ThrowException = false;
}
};var mms = new MailMergeSender { Config = ... };
mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) =>
{
lock (_locker)
{
// if you see there are too many issues:
messageFailureArgs.ThrowException = true;
}
};var mms = new MailMergeSender { Config = ... };
mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) =>
{
lock (_locker)
{
// if you found out it's a minor issue, so you send the faulty message
messageFailureArgs.MimeMessage = ((MailMergeMessageException)messageFailureArgs.Error).MimeMessage;
messageFailureArgs.ThrowException = false;
}
};