For a MOSS/SAP project we needed a simple way to exchange SAP documents (order-confirmation, invoices, ...) with MOSS. The incoming email feature came in handy. Just define an email-address for the desired doclib and tell SAP to send emails including the documents as PDF attachments - sounds easy. The doclib was configured just to store the attachments and remove the original emails. After activation of this simple interface nothing happend. No attachments could be found in the doclib. First I tought that the emails were miss-routed, but the mail-server said everything was ok. I enabled to keep the original emails on the doclib. Now a number of emails were saved, but no attachment anyway. Strange! Saving the .eml messages and opening them with Outlook, Thunderbird, ... did work - the attachments (PDFs) were included - or using vim - showed a plain MIME-message with base64 encoded attachment. But there seemed to be something wrong with the MIME-message because MOSS did not recognise the attachment. I decided to parse the message myself - using a MIME library for .NET. Google found a codeproject site with a rather good example including a complete MIME library. The Lumisoft.Net library is really easy to use. I created a small test-program to find out what's wrong with the SAP MIME-message. Soon I found the problem. It seems that the SAP MIME-message includes a number of NULL-bytes which causes the Base64 decoder to throw an Exception.
Mime message = Mime.Parse(@"..\..\SAP_MIME.eml");
byte[] bytes = message.MainEntity.DataEncoded;
// without trim decode fails with exception
Encoding enc = Encoding.Default;
string myString = enc.GetString(bytes);
myString = myString.Trim('');
byte[] decoded = Base64DecodeString(myString);
After removing the trailing Null-bytes the bas64-encoded attachment could be extracted. To process all the files in the doclib I needed a timer-job anyway so I added the simple MIME-parsing/cleanup code to the timer-job. Everyhting works like a charm know. SAP and Microsoft - some times I think those two companies don't want things to be easy.