473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Close a memorystream?

Hi
I have a function that sends email messages plus attachments.
I'm worried about the memorystream I'm using in the following clipped code
sample.
Will it be closed/disposed of without me doing anything ?
If not, should I be storing reference to the streams I create and
close/dispose of them after the message is sent?

thank you :)

// Attachments
if (Attachments != null)
{
foreach (tableMessagesEmailAttachments attachment in Attachments)
{
MemoryStream ms = new MemoryStream(attachment.AttachmentData);
System.Net.Mail.Attachment att = new Attachment(ms,
attachment.AttachmentFilename);
Message.Attachments.Add(att);
}
}
MailClient.Send(Message);
Jan 8 '08 #1
2 2443
On Jan 8, 10:30 am, "Claire" <claire@no_hope.comwrote:
I have a function that sends email messages plus attachments.
I'm worried about the memorystream I'm using in the following clipped code
sample.
Will it be closed/disposed of without me doing anything ?
If not, should I be storing reference to the streams I create and
close/dispose of them after the message is sent?
In fact, MemoryStreams don't use anything that strictly speaking have
to be disposed of. There's nothing to say they couldn't in future, and
the fact that it implements (inherently) IDisposable means it's best
practice to dispose it - but you don't absolutely, absolutely have to.

Fortunately, when you dispose a MailMessage, it will dispose all of
its attachments, which will in turn close the streams. So the question
is really: are you disposing of your MailMessage?

Jon
Jan 8 '08 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:3cfc22a1-3ece-4172-ac36-
is really: are you disposing of your MailMessage?
Thank you jon :)
Jan 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
4
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write,...
3
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream();...
9
by: Oleg Subachev | last post by:
I use local to methods MemoryStreams. Both read-only and read-write ones. Do I need to call MemoryStream.Close for read-only MemoryStreams before leaving the method ? And what about...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
7
by: Peter Morris | last post by:
Can I use a BinaryWriter without it closing the underlying stream? I want to save information to a MemoryStream using a BinaryWriter but I want to dispose of the BinaryWriter and keep the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.