Handling events

The sample demonstrates sending e-mail while staying responsive to user actions. User can abort sending process by closing the app.

Note: This sample code is for Visual Basic only. ASP does not support events.

By default, MailBee does not processes Windows events when performing mail operation. This gives maximum performance and fits fine for web applications (such as ASP ones).

Desktop applications, however, usually give user an option to interact with the app even when it is busy (e.g. sends e-mail).

To let MailBee process Windows events, EnableEvents property of SMTP object must be set to True before connecting to SMTP server.

MailBee is also capable of firing its own events (such as sending progress). See EnableEvents property documentation for details.

' Declare mailer object global to control
' it within different subroutines
Dim objSMTP

' User clicked the button. Must send e-mail
Private Sub Command1_Click()

  ' Allow SMTP object to process Windows
  ' events during operation
  objSMTP.EnableEvents = True
  
  ' Unlock SMTP component
  objSMTP.LicenseKey = "put your license key here"
  
  ' Set SMTP server name
  objSMTP.ServerName = "mail.server.com"
  
  ' Set message properties
  objSMTP.FromAddr = "me@mydomain.com"
  objSMTP.ToAddr = "you@yourdomain.com"
  objSMTP.Subject = "Hi"
  objSMTP.BodyText = "This is large message"
  
  ' Add large attachment so message sending
  ' would take some time
  objSMTP.AddAttachment "C:\Data\Bigfile.avi"
  
  ' Send it!
  objSMTP.Send
  
  ' Close the connection
  objSMTP.Disconnect
End Sub

' App launched, create mailer object but
' do not send any e-mail yet
Private Sub Form_Load()
  ' Create mailer component
  Set objSMTP = CreateObject("MailBee.SMTP")
End Sub

' User closed the app
Private Sub Form_Unload(Cancel As Integer)
  ' If e-mail is still being sent,
  ' abort mail operation
  If objSMTP.Busy Then objSMTP.Abort
End Sub

See Also:

SMTP.EnableEvents Property