Robert Oschler
Robert Oschler

Reputation: 14385

Need Delphi 6 example for updating a progress bar during TIdSmtp send operation, especially for large attachments

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?

Upvotes: 1

Views: 1529

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598134

TIdSMTP encodes 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 temporary TStream via the TIdMessage.SaveToStream() method and then grab the value of the TStream.Size property. Since you are encoding large attachments, that will take some time and lots of memory overhead. Since TIdSMTP will just re-encode the email again during transmission, there is no guarantee that the number of bytes actually transmitted will match the temp TStream.Size due 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 the AWorkMode parameter will be set to wmWrite. Since TIdSMTP.Send() does not know ahead of time how many bytes it will be sending, the AWorkCountMax parameter of the TIdSMTP.OnWorkBegin event will be 0, but at least you will know when the actual email data begin encoding/sending (after TIdSMTP has exchanged several commands with the server). The AWorkCount parameter of the TIdSMTP.OnWork event will be the total number of bytes actually sent. When the TIdSMTP.OnWorkEnd event is fired, the email has finished being sent.

Based on the temp TStream.Size property and the AWorkCount parameter of the TIdSMTP.OnWork event, 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.

Upvotes: 4

Related Questions