A Question about SMTP Send and MessageSent

Andris

Active Member
Licensed User
Longtime User
I've implemented a simple emailer in my current project using SMTP. It works perfectly whether I send a single email or multiple ones (one after another with customized content) automatically. However, when sending multiple emails, I'd like to receive the MessageSent results of any Send before moving on to sending the next email, so that I can update status of each sent email in a database before continuing with the next one. This is proving to be extremely frustrating, probably because I don't have a good understanding of how MessageSent works.

Here are the basics of my emailing code:


B4X:
Sub Process_Globals
   
   Dim SMTP As SMTP
   Dim SendStatus As String

End Sub

Sub SendEmail

   ... (set all the usual SMTP fields)
    
   SendStatus=""
   SMTP.Send

   Do Until SendStatus<>""
      DoEvents
   Loop

End Sub

Sub SMTP_MessageSent(Success As Boolean)

   If Success=False Then
      SendStatus="Failed"
   Else
      SendStatus="OK"
   End If
   
End Sub

The idea of the 'Do Until' loop in SendEmail is to make it wait until Success has reported a result before SendEmail returns to my UI (where it will be called again with data for the next email). What happens in reality is that it becomes an endless loop because SendStatus is never altered by MessageSent. Can anyone tell me why?
 

Andris

Active Member
Licensed User
Longtime User
As a follow-up to my own question, the right approach is to put the email list management right in the MessageSent code. In other words start the next email in the list from MessageSent instead of from my UI code. Otherwise, my Do loop approach keeps my activity going and prevents MessageSent from processing.

All good now! Thanks to those of you who might have been thinking about it for me.
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
Another option is to create a class with smtp obj and event in it and an I'd.

Then you can run those objects query for the status. Update the database etc an object approach
 
Upvote 0
Top