user711330
user711330

Reputation: 33

using MemoryStreams as content sources when attaching files to email, C#

So, I've been having a bit of trouble trying to attach files using a MemoryStream as a content source.

Right now, I'm fetching images from a remote web server and trying to package them into an email that functions as a daily report.

I've gotten the fetching, parsing, and image extraction all down, and I've tested that it works. The problem I'm facing is that when I create an Attachment object using a MemoryStream as the content source/content stream, the email contains a blank file/image. However, when I specify an actual file that resides on the hard drive, it attaches to the message with no problem and the message received is perfect.

Obviously, I would rather not have to cache any of the images locally before I attach the files and send them since this will create unnecessary overhead with the caching and then purging them. But, if need be, I can make this my last resort.

My question is if there is some known issue with using a MemoryStream as the contentStream when creating an Attachment object, or if there is something I'm forgetting.

Thanks!

Upvotes: 0

Views: 851

Answers (1)

spender
spender

Reputation: 120548

I'm trying my psychic debugging powers: you've loaded your MemoryStream with data and forgotten to reset position to zero before reading out of it. If so,

myMemoryStream.Position = 0

in a tactical position might help.

Upvotes: 7

Related Questions