Wednesday 8 September 2010

Email Attachments

Today I spend just a little too long trying to attach a in-memory Excel 2007 spreadsheet to an email.

I have recently started using ExtremeML to create Excel documents and today I attempted to go a step further and use the in-memory instance of the generated document as an attachment for an email.

It seemed straight forward, create a MemoryStream object and pass it to the routine in ExtremeML to fill it with the Excel document.  Then add the MemoryStream object to the MailAttachments property of the Message object and Send!  How easy can that be…

Well, I receieved the email, the attachment was there but it was empty!

Actually, Outlook was stating it was stating the attachment was 386B but when I saved the attachment to my desktop, it was actually zero bytes.  I can only assume that Outlook is reporting the size the attachment is taking up in respect to being embedding in the raw email body.

Anyhoo, I am left scratching my head for ages.  I can swap the MemoryStream for a FileStream and it successfully saves the file.  But if I use a MemoryStream it suddenly forgets the data.  I can check the stream before it attaches it to the mail message and it seems to contain data.  And then I realise…

The stream has just been written to, the pointer within the stream is pointing to the end of the stream!

   1:  attachmentStream.Seek(0, SeekOrigin.Begin);

Is all I had to do before attaching the stream as a attachment.

I fire up the test app and I dutifully receive the email with a fully populated attachment.

I win!

Tuesday 7 September 2010

Lufthansa Virtual Pilot

Not sure how good or bad this is but I seemed to be doing reasonably well for making mostly guestimates.