I am using the Indy TIdSmtp component to send E-mails. The E-mails I am sending will have a large attachment, usually in the range of 5 to 40 MB. I want to update a progress bar that will show the overall progress of the send as a percentage of the total number of bytes that need to be sent. I don’t care if it’s really precise, just good enough to give someone watching the progress bar an indication for how far along the overall E-mail sending process is.
Can someone point me to a code sample that shows me how to do this?
TIdSMTPencodes the email on-the-fly as it is being sent to the server. The total number of bytes being sent is not known ahead of time. The only way you would be able to determine a value even reasonably close is to encode the email to a temporaryTStreamvia theTIdMessage.SaveToStream()method and then grab the value of theTStream.Sizeproperty. Since you are encoding large attachments, that will take some time and lots of memory overhead. SinceTIdSMTPwill just re-encode the email again during transmission, there is no guarantee that the number of bytes actually transmitted will match the tempTStream.Sizedue to the dynamic nature of various email headers, such as timestamps and MIME boundaries.To determine how many bytes are actually being sent, use the
TIdSMTP.OnWork...events, where theAWorkModeparameter will be set towmWrite. SinceTIdSMTP.Send()does not know ahead of time how many bytes it will be sending, theAWorkCountMaxparameter of theTIdSMTP.OnWorkBeginevent will be0, but at least you will know when the actual email data begin encoding/sending (afterTIdSMTPhas exchanged several commands with the server). TheAWorkCountparameter of theTIdSMTP.OnWorkevent will be the total number of bytes actually sent. When theTIdSMTP.OnWorkEndevent is fired, the email has finished being sent.Based on the temp
TStream.Sizeproperty and theAWorkCountparameter of theTIdSMTP.OnWorkevent, you will be able to display an approximation of a percentage for a progress bar. It will not be guaranteed to be 100% accurate, but it will be close.