Tuesday, October 19, 2010

Email attachments - using memory stream

In my previous post I mentioned about a task where I had to send HTML mails. In each of the emails, I also had to attach a file. The files were dynamically created and I wanted to avoid having to physically create the files on the servers, attach the files to the mail and then having to delete it.

I found that it is possible to attach files as memory streams using
Message.Attachments.Add(memoryStream, ...

But as I wrote the code to do this, I found that the size of the file been attached was always 0 kb. So back to the trouble shooting ..

I was creating the file in a different assembly and the file was been received as a memory stream. So the code looked something like this

Class A
- Get details to create the file from database
- Call a method in Class B to create the file and store the returned memory stream in local variale
- Call a method in class C to send mails and pass the local memory stream to this method

Class B
- create a new file and return the file as memory stream object

Class C
- Create a mailmessage.
- Attach the file received as memorystream parameter to the mail message
- send the mail.

Everything seemed right. I even debugged the code to ensure that the memorystream object been created in class B was not empty. I debugged to see if the memory stream received by the method in class C had all the proper content and was not empty. Again everything looked fine. So where was the problem. Various people on the internet seemed to suggest flusing the memomorystream, moving the position to 0, etc but nothing worked

I finally realized that when files are attached as a memory stream to emails, the stream needs to be open. So first I tried by not closing the stream in Class B. But that did not work. So in class C before attaching the file to the mail message, I created a new stream using the same stream I got as a parameter, like this

MemoryStream memoryStream = new MemoryStream(pMyStream.ToArray());
MyMailMessage.Attachments.Add(new Attachment(memoryStream, "Name.pdf", MediaTypeNames.Application.Pdf));


and that worked !!

Line Breaks does not appear in HTML emails

Its been a looong time since my last post. That doesn't mean that I have been idle and not working :-). Just that I have been busy (which is good) and also postponing my task of updating this blog (which is bad).

There have been various issues that I have faced in the last year or so and solutions I found out the hard way which I want to write about and hopefully, I get it done sooner than later.

Anyway to start of, I will start of with a very trivial issue that some of you might have never noticed, but something which stumped me for couple of hours.

I had a task to send HTML mails using Lotus notes and I had a tough time getting the line breaks to appear. I checked all the usual things to ensure that the mails are marked as HTML and still the line breaks would not appear.

I thought it is a lotus notes client issue until I finally found what I was doing wrong.
HTML tag for line break is <br> and not <br/>.


In HTML the <br> tag has no end tag.
But in XHTML the <br> tag must be properly closed like <br />.