Ignore Exceptions When Loading Invalid Data in C#

Email files created by different applications do not always follow the specifications. A message may contain a damaged TNEF attachment (winmail.dat), an iCalendar file may contain a recurrence rule with an invalid value, and so on. By default, Aspose.Email throws an exception for such data. For some of these cases, the library can skip the invalid part and continue loading.

The ExceptionManager class in the Aspose.Email.Exceptions namespace controls this behavior. It lets you ignore all ignorable exceptions, ignore only selected ones, or decide case by case in a callback.

Recognize an Ignorable Exception

When an exception can be ignored, its message tells you so and shows the path to add to IgnoreList. For example, loading an ICS file that contains RRULE:FREQ=DAILY;INTERVAL=0;COUNT=3 with Appointment.Load throws an AsposeException with the following message:

Incorrect option value of a recurrent rule: 'INTERVAL'.

If you want to ignore an exception and want to proceed further then you can use:
ExceptionManager.IgnoreList.Add(@"RecurrenceRule\IncorrectIntervalRulePart")
The INTERVAL rule part should be a positive integer.

The first line describes the problem. The ExceptionManager.IgnoreList.Add(...) line gives the path of this case, and the last line explains what the library does when the exception is ignored.

Each ignorable case has a path in the {Module}\{Method}\{Action}\{GUID} format, for example, MailMessage\Load\DecodeTnefAttachment\64149867-679e-4645-9af0-d46566cae598. The exception message shows the path without the GUID.

What Can Be Ignored

  • Only exceptions whose message contains the ExceptionManager.IgnoreList.Add(...) hint can be ignored. All other exceptions are always thrown, whatever the ExceptionManager settings are. This includes argument validation, such as a null or empty file name, a malformed email address, or a missing file.
  • The library currently supports the following cases:
Case Path When ignored
A damaged TNEF attachment (winmail.dat) when loading a MailMessage MailMessage\Load\DecodeTnefAttachment The attachment is kept as a regular attachment.
An invalid rule part in an iCalendar recurrence rule, for example, INTERVAL=0 RecurrenceRule\IncorrectIntervalRulePart The invalid rule part is dropped. FREQ=DAILY;INTERVAL=0;COUNT=3 is loaded as FREQ=DAILY;COUNT=3.
Damaged data headers when reading OLM files Shown in the exception message Described in the exception message.

New versions of the library may add more cases. Always take the path from the exception message.

Ignore All Ignorable Exceptions

Set the IgnoreAll property to true to ignore every exception that can be ignored:

using Aspose.Email.Calendar;
using Aspose.Email.Exceptions;

ExceptionManager.IgnoreAll = true;

// The invalid INTERVAL rule part is dropped, the appointment is loaded
var appointment = Appointment.Load("event.ics");

Ignore Selected Exceptions

To ignore only some cases, add their paths from the exception messages to IgnoreList. An entry also covers every path that starts with it. For example, RecurrenceRule covers all recurrence rule cases.

using Aspose.Email;
using Aspose.Email.Calendar;
using Aspose.Email.Exceptions;

ExceptionManager.IgnoreList.Add(@"RecurrenceRule\IncorrectIntervalRulePart");
ExceptionManager.IgnoreList.Add(@"MailMessage\Load\DecodeTnefAttachment");

var appointment = Appointment.Load("event.ics");
var message = MailMessage.Load("message-with-damaged-tnef.eml");

Decide in a Callback

For full control, set the IgnoreExceptionsHandler callback. The library calls it for every ignorable exception and passes the exception and its path. Return true to ignore the exception or false to throw it.

The callback receives the full path, including the action and the GUID. So compare the beginning of the path with StartsWith. An exact comparison such as path == @"MailMessage\Load" never matches.

using Aspose.Email;
using Aspose.Email.Exceptions;

// path: {Module}\{Method}\{Action}\{GUID}
// for example: MailMessage\Load\DecodeTnefAttachment\64149867-679e-4645-9af0-d46566cae598
ExceptionManager.IgnoreExceptionsHandler = (ex, path) =>
{
    // Ignore all ignorable exceptions raised by MailMessage.Load
    return path.StartsWith(@"MailMessage\Load\");
};

var message = MailMessage.Load("message-with-damaged-tnef.eml");

Log Ignored Exceptions

To keep track of what was skipped, set the IgnoreExceptionsLogHandler callback. It receives the message of every ignored exception, whichever of the methods above was used to ignore it.

The following example loads an ICS file that contains RRULE:FREQ=DAILY;INTERVAL=0;COUNT=3 and shows the three ways to ignore the exception. Use one of them.

using System;
using Aspose.Email.Calendar;
using Aspose.Email.Exceptions;

ExceptionManager.IgnoreExceptionsLogHandler = message => Console.WriteLine("Ignored: " + message);

// either ignore all ignorable exceptions
ExceptionManager.IgnoreAll = true;

// or only selected ones, using the path from the exception message
ExceptionManager.IgnoreList.Add(@"RecurrenceRule\IncorrectIntervalRulePart");

// or decide in a callback (IgnoreAll and IgnoreList are then not used)
ExceptionManager.IgnoreExceptionsHandler = (ex, path) => path.StartsWith(@"RecurrenceRule\");

// The appointment is loaded, the invalid INTERVAL rule part is dropped
var appointment = Appointment.Load("event.ics");
Console.WriteLine(appointment.Recurrence); // FREQ=DAILY;COUNT=3

Restore the Default Behavior

ExceptionManager settings are static. They apply to the whole application, including all threads, until you change them. To throw all exceptions again, reset the settings:

using Aspose.Email.Exceptions;

ExceptionManager.IgnoreAll = false;
ExceptionManager.IgnoreList.Clear();
ExceptionManager.IgnoreExceptionsHandler = null;
ExceptionManager.IgnoreExceptionsLogHandler = null;