How can I get the sizes of the attachments of a mail message? The following code throws an exception: System.ObjectDisposedException: Cannot access a closed Stream public string[] GetPropertiesAttachmentSizes(int i)
{ string[] attachmentSizes = null; int counter = 0; foreach (var attachment in CurrentMessage.Attachments) { if (attachment.ContentType.MediaType.Equals(“message/rfc822”) == false) { // System.ObjectDisposedException: Cannot access a closed Stream. attachmentSizes[counter++] = attachment.ContentStream.Length.ToString(); } }
We have tested the usage of the sample code with a number of attachments at our end and every time it returns the correct size of the attachment. Could you please check if the ContentStream of the attachment is valid and is not null when you are getting an exception? If possible, please share some sample files with us that exhibit this issue.
The “using” statement takes care of any memory leaks automatically. In order to read the contents of the attachments, you may save these using the Attachment’s Save method as well as shown in the following code sample. Please let us know if you need further assistance in this regard. Sample Code:
MemoryStream ms = new MemoryStream(); att.Save(ms); ms.Position = 0; byte[] byteArr = ms.ToArray();
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.