Click here to Skip to main content
Click here to Skip to main content

Back to Basics – Reading a File into Memory Stream

By , 3 Mar 2011
 

Back to Basics – Reading a File into Memory Stream

Today I was asked to help a developer with a simple task she had. That task included reading an image file into a memory stream in order to send the image through an e-mail attachment. This post will show you how to do exactly that.

Reading a File into Memory Stream

Here is the code for reading the file into a memory stream:

using (FileStream fileStream = File.OpenRead(filePath))
{
    MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}

That’s it for the reading part. Pay attention to use the using statement in order to dispose the FileStream after you use it.

Adding a MemoryStream as Attachment to a MailMessage

In order to use a stream as an attachment for an e-mail message object all you have to do is to write the following code:

msg.Attachments.Add(new Attachment(memStream, filename, MediaTypeNames.Image.Jpeg));

In the code sample msg is an instance of a MailMessage class, memStream is the MemoryStream (such as the memory stream from the previous code sample) and filename is the name of the file in the attachment. Since I know that the image is jpeg then I use the MediaTypeNames.Image.Jpeg.

Summary

Reading a file into a stream is a very basic thing to know. In the post I showed how to read an image into a memory stream and also how to attach it into a mail message object.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink
Architect Sela Group
Israel Israel
Member
Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberwtwhite7 Mar '11 - 16:27 
GeneralRe: My vote of 1memberGil Fink12 Mar '11 - 4:08 
GeneralRe: My vote of 1memberwtwhite12 Mar '11 - 23:56 
I'm sorry I still don't understand. If you can read the file from the "restricted fileserver" using a FileStream, then you could just pass that same FileStream to the Attachment constructor directly. Can you explain why that would not work?
GeneralRe: My vote of 1memberGil Fink17 Mar '11 - 3:25 
GeneralRe: My vote of 1memberwtwhite18 Mar '11 - 4:14 
General[Nevermind]mvpAspDotNetDev3 Mar '11 - 10:51 
GeneralRe: This is a Tip/TrickmemberGil Fink4 Mar '11 - 0:00 
GeneralRe: This is a Tip/TrickmvpAspDotNetDev4 Mar '11 - 11:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 3 Mar 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid