Atozed Forums

Full Version: Get size of TIdMessage without first saving to TStream
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

We all know we can get the size of a TidMessage instance by first saving it to a TMemoryStream and read TMemoryStream.Size to get the size of the email message in bytes, but this doesn't sound performance-friend.

Is there an alternative way to get the size of a Tidmessage object?

Thanks.
Indy has a TIdCalculateSizeStream class in the IdGlobal unit. It acts similar to TMemoryStream but without using an actual memory buffer. It is intended to be used for exactly this kind of scenario - counting the number of bytes "written" to it. You can use it in the exact same way as before - save the TIdMessage to it, and then read its Size property.

Just note that TIdMessage generates its stream data dynamically, so there is no guarantee that the byte count will be exactly the same number each time you call its SaveTo...() methods, even if you don't change any of its properties in between calls. Particularly in regards to timestamps, message part boundaries, etc. So calculating the size of a TIdMessage is more of an estimate than an absolute.
@rlebeau, Great! Thanks, will try it.