Close a memorystream?

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

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);
 
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
 
Back
Top