Click here to Skip to main content
12,450,101 members (52,521 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

80.9K views
1 bookmarked
Posted

Back to Basics – Reading a File into Memory Stream

, 3 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
wtwhite7-Mar-11 16:27
memberwtwhite7-Mar-11 16:27 
GeneralRe: My vote of 1 Pin
Gil Fink12-Mar-11 4:08
memberGil Fink12-Mar-11 4:08 
GeneralRe: My vote of 1 Pin
wtwhite12-Mar-11 23:56
memberwtwhite12-Mar-11 23:56 
GeneralRe: My vote of 1 Pin
Gil Fink17-Mar-11 3:25
memberGil Fink17-Mar-11 3:25 
Hi,

Since the application can't read the file from the file server directly (it is a restriction of IT department in that enterprise).
The data is exposed by a service which moves the file's binary data to the application server.
I can tell you that this isn't a nice implementation but sometimes you are bound to the restrictions or demands of the enterprise you work or consult for.
GeneralRe: My vote of 1 Pin
wtwhite18-Mar-11 4:14
memberwtwhite18-Mar-11 4:14 
General[Nevermind] Pin
AspDotNetDev3-Mar-11 10:51
mvpAspDotNetDev3-Mar-11 10:51 
GeneralRe: This is a Tip/Trick Pin
Gil Fink4-Mar-11 0:00
memberGil Fink4-Mar-11 0:00 
GeneralRe: This is a Tip/Trick Pin
AspDotNetDev4-Mar-11 11:57
mvpAspDotNetDev4-Mar-11 11:57 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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