0 votes

I have one mail that seems to have an encoded attachment with invalid Base-64 character(s).

When trying to retrieve the attachment with method GetDataByUID(), I just get null:

BodyStructure structure = ipimap.GetBodyStructureByUID(mailID);
foreach(MimeStructure attachment in structure.Attachments)
    byte[] buffer = imap.GetDataByUID(attachment);

I.e. the buffer is null after the call to GetDataByUID()!

When trying to get the attachment with another mail tool, it responses that:
"Invalid character in a Base-64 string."
I.e. the attachment seems to not be correct.

Is there a way to detect this "invalid character" error with the Mail.dll component?
There seems to be no exceptions thrown from the Mail.dll that I could use.

The log output looks as follows when GetBodyStructureByUID and GetDataByUID is called:

Mail.dll:  9 07:11:09 3.0.14088.1309
Mail.dll:  9 07:11:09 S: * OK The Microsoft Exchange IMAP4 service is ready.
Mail.dll:  9 07:14:18 S: * 1 FETCH (UID 882 BODYSTRUCTURE 
(("text" "html" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 
3748 110 NIL NIL NIL NIL)("application" "octet-stream" NIL "<>" NIL     
"base64" 0 NIL ("attachment" ("filename" "facture.pdf")) NIL NIL) 
"mixed" ("boundary" "part__95062704_1618_48B1_AC6B_B632A2661") NIL 
Mail.dll:  9 07:14:18 S: e73ebe47e99b4c9a OK FETCH completed.
Mail.dll:  9 07:14:18 C: 00631adc3c034c05 UID FETCH 882 (UID BODY[2])
Mail.dll:  9 07:14:18 S: * 1 FETCH (UID 882 BODY[2] NIL FLAGS (\Seen))
Mail.dll:  9 07:14:18 S: 00631adc3c034c05 OK FETCH completed.

The Base-64 encoded attachment, and its size, should be shown in the log instead of NIL, in line:

Mail.dll:  9 07:14:18 S: * 1 FETCH (UID 882 BODY[2] NIL FLAGS (\Seen))
edited by

1 Answer

0 votes

It is the IMAP server, that is not returning the data you asked for. Most likely it is unable to parse the message. There is not much Mail.dll can do about it. If it receives no data it returns null - this is correct and expected behavior.

What happens if you download entire message using Mail.dll (GetMessageByUID)? Are you able to parse it and extract the attachment? Mail.dll has a very tolerant Base64 parser.

by (286k points)
GetMessageByUID is not able to get the whole mail content!
For the attachment part it returns:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="facture.pdf"
Content-ID: <>
Content-Transfer-Encoding: base64


So it seems like that the mail server is not able to return the part at all, if it contains invalid characters!
Maybe this massage was not sent this way?
When forwarding the mail with Outlook to my self, Mail.dll is able to retrieve the attachment with GetDataByUID()!

For ex GetMessageByUID now returns for the attachment part following:

Content-Type: application/octet-stream; name="facture.pdf"
Content-Description: facture.pdf
Content-Disposition: attachment; filename="facture.pdf"; size=202967;
    creation-date="Tue, 05 Aug 2014 07:58:32 GMT";
    modification-date="Thu, 24 Jul 2014 18:22:41 GMT"
Content-ID: <D20105CCCF12DA45A8CFAF9690377F4C@mittalco.com>
Content-Transfer-Encoding: base64



So it seems like that the mail server (Exchange) is able to repair invalid characters in Base-64 encoded parts when the mail is forwarded!

But my original problem was how to detect this error with the Mail.dll component.
If it is not possible, I can live with using the returned null value by the GetDataByUID() method to report to the end user that we could not retrieve the attachment.
If the server is not returning the data, there is nothing you can do. You can just report missing attachment error to the user.