A bug and a suggestion

Hello

According to:

IgnoreAll: Ignore all exceptions.

Either docs are wrong/outdated or after some point the devs forgot to cover this for most parts of the SDK!

And if you don’t mind please?

MapiMessage.Headers.Set(“X-Mailer”, GetXMailer())

If GetXMailer() returns an empty string, then Headers.Set throws:

The parameter ‘value’ cannot be an empty string.

Parameter name: name

Can you swallow the exception and just ignore it?

Even IgnoreAll Will not affect this:

Aspose.Email.Exceptions.ExceptionManager.IgnoreAll = True

Perhaps ExceptionManager.IgnoreAll = True is not applied everywhere constantly!

However, my request is to ignore:

MapiMessage.Headers.Set(“X-Mailer”, “”)

Regardless of ExceptionManager.IgnoreAll value!

At least, it must honor IgnoreAll = True

Am I correct? :slight_smile:

Hello,

ExceptionManager is not a global try/catch around the whole API, and it never was. It works only at specific points where the library reads damaged or non-standard input data and has a safe way to continue. At such a point, the exception message says that the error can be ignored and gives the path to add to IgnoreList, for example:

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.

IgnoreAll = True ignores all exceptions of this kind at once, so you don’t have to add each path to IgnoreList. Currently these are:

  • an invalid TNEF (winmail.dat) attachment when a message is loaded;
  • invalid data headers when an OLM file is read;
  • invalid recurrence rules (INTERVAL, COUNT, start date) in calendar items.

Argument checks are outside this scope on purpose. An empty or null argument means the calling code has an error, and the library has no safe default it could pick silently. That’s why these calls throw a standard ArgumentException (not an AsposeException) whatever the IgnoreAll value is:

msg.Headers.Set("X-Mailer", "")   ' ArgumentException
msg.Headers.Set("", "value")      ' ArgumentException
Dim a As New MailAddress("")      ' ArgumentException

We agree that the API reference description “Ignore all exceptions.” is misleading. We have logged a task to describe the real scope there.

Headers.Set with an empty value

Here you are right, and we found two issues:

  • The exception reports the wrong parameter name. The message says 'value', but ParamName is name, which is why you see “Parameter name: name”. We logged it as EMAILNET-41900.
  • The behavior isn’t consistent. Headers.Add("X-Mailer", "") accepts an empty value and simply removes the X-Mailer header, but Headers.Set("X-Mailer", "") throws. We logged EMAILNET-41901 to make Set handle an empty value the same way, without an exception and regardless of ExceptionManager settings. This fixes the problem at its source instead of hiding the exception.

Until this is done, please check the value before you call Set:

Dim xMailer As String = GetXMailer()

If String.IsNullOrEmpty(xMailer) Then
    msg.Headers.Remove("X-Mailer")       ' does nothing if the header isn't there
Else
    msg.Headers.Set("X-Mailer", xMailer)
End If

If you set headers in several places, you can wrap this in a helper:

Imports System.Runtime.CompilerServices
Imports Aspose.Email.Mime

Module HeaderExtensions
    <Extension()>
    Public Sub SetOrRemove(headers As HeaderCollection, name As String, value As String)
        If String.IsNullOrEmpty(value) Then
            headers.Remove(name)
        Else
            headers.Set(name, value)
        End If
    End Sub
End Module

' usage
msg.Headers.SetOrRemove("X-Mailer", GetXMailer())

Thank you.