Some attachments with incorrect DispositionType

In one of my mails (file extension “msg”) an attachment is loaded into the MailMessage.Attachments with the DispositionType “Inline” although it is attached as a file.
When creating a new mail with the same attachment it has the correct DispositionType (“Attachment”).

I encountered this issue with some different mails and attachments of different file types but sadly I don’t have a reliable reproduction. How does Gembox determine if an attachent is inlined?

I need the information to handle attached files for a mail and I need to ignore inlined attachments like pictures with embedded links in a signature.
Is there another way to differentiate whether an attachment is embedded in the mail text?

For further investigation I can provide you with one of the mails, if that helps.

Hi Sarah,

Yes, please send us your MSG file so that we can investigate it.

Regards,
Mario

Hi Mario,

thanks for your quick reply. I sent the file via mail.

Kind regards,
Sarah

One other thing I noticed: the same mail as eml-file instead of msg does not have the same issue. It seems to appear only in the msg file.

I have similar issues (Attachment.DispositionType is wrong) with some MailMessages loaded from “.msg” files.

Are there any news regarding this issue? Do you need additional samples?

Hi Stephan,
We’re currently working on this, I’ll write again later after we’re done.
Regards,
Mario

Hi Mario,

thanks for your reply :slight_smile:

If there is anything I can do to help (e.g. testing a preview version of a potential fix), don’t hesitate to contact me.

Kind regards,
Stephan.

Hi Sarah and Stephan,

We investigated the attachment in question and we believe that the type is correct even though it is not part of the body.

You see, the thing is that this attachment specifies the PidTagAttachContentId property which is an identifier to be used as a reference on the message’s HTML body. So if it has it, GemBox.Email interprets it as a potential inline attachment.

Perhaps as a workaround, you could check if that Content ID exists in the HTML body.
For example, something like this:

foreach (var attachment in message.Attachments)
{
    var dispositionType = attachment.DispositionType != ContentDispositionType.Inline ||
        message.BodyHtml.Contains($"cid:{attachment.ContentId}") ?
        attachment.DispositionType : ContentDispositionType.Attachment;

    // ...
}

I hope this helps.

Regards,
Mario

1 Like

Hi Mario,

thank you for your quick reply.

I tried your workaround and it looks good at so far, except one (maybe unrelated?) “issue”: sometimes I’m getting an attachment with disposition type Attachment, although it isn’t a “real” attachment. In this case I get an attachment called “Body.rtf” which contains the RTF body of the MailMessage. Is this also by design? Is there a better way to ignore this “fake” attachment than skipping any attachment named "Body.rtf?

Interestingly MsgReader appears to be doing something similar to the proposed workaround internally: MSGReader/Message.cs at 5d563c0c699fde70218929768f08f4256af98ef3 · Sicos1977/MSGReader · GitHub. Are you planning to do something similar in an upcoming version of GemBox.Email? Otherwise one has to remember to use the above workaround every time.

Kind regards,
Stephan.

Hi Mario,

I tried the workaround too, but I received a NullReferenceException on the message.BodyHtml property. I loaded the same mail that I sent you for reproduction.
Is this due to using the Free License limitations or is the loading process different to receive the BodyHtml?

Kind regards,
Sarah

Hi Sarah,

maybe your mail doesn’t have an HTML body and is therefore null? At least that’s what happened in some of my test cases.

At the moment I’m using the following extension method, which “fixes” Attachment.DispositionType and also handles the case where BodyHtml is null:

public static void FixAttachmentDisposition(this GemBox.Email.MailMessage message)
{
	if (message?.Attachments.Count > 0)
	{
		var bodyHtml = message.BodyHtml ?? "";
		foreach (var attachment in message.Attachments)
		{
			if (attachment.DispositionType == ContentDispositionType.Inline &&
				!bodyHtml.Contains($"cid:{attachment.ContentId}"))
			{
				attachment.MimeEntity.Disposition = "attachment";
			}
		}
	}
}

I hope this helps.

Kind regards,
Stephan.

1 Like

Hi Stephan,

Works fine for my cases too :slight_smile: Thanks!

This workaround fixes my use case, this issue is closed for me - should I leave it open for your questions regarding the RTF-body @Stephan?

Kind regards,
Sarah

Hi,

The “Body.rtf” is raw RTF content, the one that doesn’t have an HTML representation. For now GemBox.Email represents or stores such body as an attachment with a predefined “Body.rtf” name.

If the MailMessage.BodyHtml is null and there is a “Body.rtf” attachment, then you could perhaps check if the $"cid:{attachment.ContentId}" exists in it.

Last, that is interesting, I didn’t know MsgReader did that.
But anyway, for now, we don’t plan to add this to GemBox.Email.
However, if we get more reports related to this matter we will consider it.

Regards,
Mario

1 Like

Hi Sarah,

now that Mario filled in the last missing bit I think this question can be closed :+1:

Kind regards,
Stephan.

Hi Mario,

thank you for your response. I’ll adjust our attachment handling accordingly.

Kind regards,
Stephan.