+3 votes

Today one of our customer reported that some of the email messages cannot be processed. It turns out that if message have zero-length attachment, calling GetMimePartByUID fails with System.NullReferenceException.

We are using Mail.dll version 3.0.13092.1409 (licensed) but I downloaded the latest 3.0.16028.1145 and it fails too...


Mail.dll: 35 14:07:13 C: 7194aa5a17ef4ab8 UID FETCH 1311 (UID RFC822.SIZE INTERNALDATE FLAGS ENVELOPE BODYSTRUCTURE)
Mail.dll: 35 14:07:13 S: * 12 FETCH (UID 1311 RFC822.SIZE 43542 INTERNALDATE "22-Feb-2016 12:49:25 +0100" FLAGS (\Recent) ENVELOPE ("Mon, 22 Feb 2016 11:49:18 +0000" "FW: it.smartSCAN - Testing UDG MailREader problem - Zero-sized PDF attachment" (("Georgi Yordanov Ganchev" NIL "Georgi.Yordanov.Ganchev" "evry.com")) NIL NIL (("rechnung.dev-ocr@udg.de" NIL "rechnung.dev-ocr" "udg.de")) NIL NIL NIL "<58B300820067624EBBA8387E73D0B5B3967D7C0A@CCDEX016.corp.corpcommon.com>") BODYSTRUCTURE ((("text" "plain" ("charset" "utf-8") NIL NIL "base64" 666 9 NIL NIL NIL NIL)("text" "html" ("charset" "utf-8") NIL NIL "base64" 5120 66 NIL NIL NIL NIL) "alternative" ("boundary" "_000_58B300820067624EBBA8387E73D0B5B3967D7C0ACCDEX016corpcor_") NIL NIL)("application" "pdf" ("name" "Zero-Sized-PDF.PDF") NIL "Zero-Sized-PDF.PDF" "base64" 0 NIL ("attachment" ("filename" "Zero-Sized-PDF.PDF" "creation-date" "Mon, 22 Feb 2016 11:38:00 GMT" "modification-date" "Mon, 22 Feb 2016 11:38:00 GMT")) NIL NIL) "mixed" ("boundary" "_004_58B300820067624EBBA8387E73D0B5B3967D7C0ACCDEX016corpcor_") NIL "en-US"))
Mail.dll: 35 14:07:14 S: 7194aa5a17ef4ab8 OK FETCH completed.
Mail.dll: 35 14:07:14 C: 9fd1ae9bc8584fb1 UID FETCH 1311 (UID BODY[1.1])
Mail.dll: 35 14:07:14 S: * 12 FETCH (UID 1311 BODY[1.1] {666}
Mail.dll: 35 14:07:14 S: DQoNCkdlb3JnaS5Zb3JkYW5vdi5HYW5jaGV2QGV2cnkuY29tPG1haWx0bzpHZW9yZ2kuWW9yZGFu
b3YuR2FuY2hldkBldnJ5LmNvbT4NClNtYXJ0U0NBTiBEZXZlbG9wbWVudCBUZWFtDQpNb2JpbGU6
ICszNTkgODk1IDQ4NSA5MDYNCg0KRnJvbTogR2VvcmdpIFlvcmRhbm92IEdhbmNoZXYNClNlbnQ6
IE1vbmRheSwgRmVicnVhcnkgMjIsIDIwMTYgMTozOCBQTQ0KVG86ICdyZWNobnVuZy5kZXYtb2Ny
QHVkZy5kZScgPHJlY2hudW5nLmRldi1vY3JAdWRnLmRlPg0KU3ViamVjdDogaXQuc21hcnRTQ0FO
IC0gVGVzdGluZyBVREcgTWFpbFJFYWRlciBwcm9ibGVtIC0gWmVyby1zaXplZCBQREYgYXR0YWNo
bWVudA0KDQoNCg0KR2VvcmdpLllvcmRhbm92LkdhbmNoZXZAZXZyeS5jb208bWFpbHRvOkdlb3Jn
aS5Zb3JkYW5vdi5HYW5jaGV2QGV2cnkuY29tPg0KU21hcnRTQ0FOIERldmVsb3BtZW50IFRlYW0N
Ck1vYmlsZTogKzM1OSA4OTUgNDg1IDkwNg0KDQo=

Mail.dll: 35 14:07:14 S: )
Mail.dll: 35 14:07:14 S: 9fd1ae9bc8584fb1 OK FETCH completed.
The thread 0x2364 has exited with code 0 (0x0).
The thread 0x3770 has exited with code 0 (0x0).
The thread 0x2ccc has exited with code 0 (0x0).
Mail.dll: 35 14:07:32 C: 5b8512f6dfe742c3 UID FETCH 1311 (UID BODY[2])
Mail.dll: 35 14:07:32 S: * 12 FETCH (UID 1311 BODY[2] NIL)
Mail.dll: 35 14:07:32 S: 5b8512f6dfe742c3 OK FETCH completed.
Exception thrown: 'System.NullReferenceException' in Mail.dll
by
Could you please turn on on logging:
https://www.limilabs.com/blog/logging-in-mail-dll

What is the entire stack trace?
The problem occurs when ContentTransferEncoding of MimeStructure (the attachment) is base64 but the Size is 0. I tested by sending zero-length PDF file to another IMAP server and it responds that the attachments is ContentTransferEncoding 7bit (again Size i 0) - then GetMimePartByUID does not throw exception.

1 Answer

0 votes

Are you sure that this happens with the latest version?
I'm 99% sure this problem no longer occurs.

This is the unit test we use to verify. It connects to outlook.com.

MailBuilder builder = new MailBuilder();
builder.Subject = "Zero sized attachment";
MimeData attachment = builder.AddAttachment(new byte[] { });
attachment.FileName = "zero-sized.dat";
attachment.ContentTransferEncoding = MimeEncoding.Base64;
IMail mail1 = builder.Create();

client.SelectInbox();

client.UploadMessage(mail1);

List<long> uids = client.GetAll();
List<MessageInfo> infos = client.GetMessageInfoByUID(uids);

MimeStructure first = infos[0].BodyStructure.Attachments[0];

Assert.AreEqual(0, first.TransferSize);
Assert.AreEqual(MimeEncoding.Base64, first.ContentTransferEncoding);

Assert.IsNull(client.GetDataByUID(first));
Assert.IsNull(first.Data);

Server acts in the same way (NIL is returned):

C: 09b755b16af94a54 UID FETCH 1216 (UID BODY[2])
S: * 1 FETCH (UID 1216 BODY[2] NIL FLAGS (\Seen \Recent))
S: 09b755b16af94a54 OK FETCH completed.
by (301k points)
edited by
Sorry, I thought that simply replacing the DLL file means the new one will be used, which was not true in my case (still the old one was used).

The methods of 3.0.16028.1145 regarding extracting attachments are so different compared to our current version 3.0.13092.1409 so that I have decided to keep my code unchanged (since we haven't problems for 2 years with it) and not upgrade to 3.0.16028.1145. I simply check if the size of attachment is 0 and skip the call to GetMimePartByUID.
Those differences are not that big as the seem:

1. You simply pass entire MimeStructure to GetDataByUID(MimeStructure structure):

byte[] attachment = client.GetDataByUID(structure.Attachments[0]);

2. GetMessageByUID returns byte[] array now.

If you have any problems converting your code, ask a new question here - we'll be glad to help.
...