Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, SMTP Simple Mail Transfer Protocol (SMTP) is the standard protocol for email services on a TCP/IP network. SMTP provides the ability to send and receive email messages. SMTP is an application-layer protocol that enables the transmission and delivery of email over the Internet. Learn More,

unable to access same MemoryStream multiple times, getting the MemoryStream closed on second attempt in C#?

Description:I am receiving a MemoryStream from a method and then I have to send it in email as attachment multiple times, i.e I have to utilise the same MemoryStream multiple time, it is working perfectly fine at first attempt but when I try to access it again it gives me error.
is there a way to store the MemoryStream separately for the purpose?
My current code looks like


MemoryStream memoryStream = getMemoryStream();
//following line works fine
SendEmail(memoryStream);
//but following line throws error
SendEmail(memoryStream);

Posted by: | Posted on: Sep 11, 2018

1 answers

Replies

3

yes, you can create a copy of the MemoryStream and hold it in a new MemoryStream variable then you can use it for some other purpose as well. every time to want to utilise it you can create a new copy. For example you can use the following code chuck.

MemoryStream memoryStreamActual = getMemoryStream();

MemoryStream memoryStreamCopy= new MemoryStream();
memoryStreamActual.CopyTo(memoryStreamCopy);

memoryStreamActual.Position = 0;
memoryStreamCopy.Position = 0;

SendEmail(memoryStreamActual);
SendEmail(memoryStreamCopy);

Replied by: | Replied on: Sep 11, 2018



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview