ComponentPro UltimateMail

      Using events

      Language Filter: AllSend comments on this topic to ComponentPro

      The Imap, Pop3 and Smtp class allow easy control mailing operations using events. This topic illustrates how to "wire-up" an event using Visual Studio environment and "wire-up" an event by yourself (if you are using the Mail component as a class instead of adding it to the project as a component).

      Wiring-up an event using Visual Studio environment

      For C#

      1. Create a new C# project and add UltimateMail component to your form. See Creating a WinForms application for information of how to do this.
      2. Click the component icon which was created on the panel underneath the form. Right click on it and click on the Properties... menu.
      3. You should be represented with the Properties Window, click on the 'lightning bolt' icon to display all events for the IMAP component.
      4. Double-click the event you wish to "wire-up".
        win
      5. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the Progress event as an example).
        void client_Progress(object sender, ImapProgressEventArgs e)
        {
            // Show progress information. 
            if (e.Length > 0 && e.State == ImapTransferState.Downloading)
            {
                Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage);
            }
        }
        
      6. Now you can connect to the IMAP server and show progress information while downloading or uploading messages in the Progress event handler.

      For VB.NET

      1. Create a new VB.NET project and add UltimateMail component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to View then select Code to switch to code view.
      3. From the "drop-down" boxes at the top of the Code Window, select the instance of the IMAP component from the box on the left, then the name of the event you would like to "wire-up" from the box on the right.
      4. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the Progress event as an example).
        vb
        Private Sub client_Progress(ByVal sender As Object, ByVal e As ImapProgressEventArgs)
            ' Show progress information. 
            If e.Length > 0 AndAlso e.State = ImapTransferState.Downloading Then 
                Console.Write(vbCr & "Downloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage)
            End If 
        End Sub
        
      5. Now you can connect to the IMAP server and show progress information while downloading or uploading messages in the Progress event handler.

      Wiring-up an event as a class

      For C#

      1. Create a new C# project and add UltimateMail component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the IMAP component such as:
        // Create a new Imap instance.
        Imap client = new Imap();
        
        // Connect to the IMAP server.
        client.Connect("myserver");
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up". For the purposes of this tutorial, the Imap.Progress event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        // Register an event handler.
        client.Progress += client_Progress;
        
      5. Navigate to the reference listing for the Progress delegate. This listing will tell you what your event-handler method should look like. According to the EventHandler<ImapProgressEventArgs> delegate, your method must accept two arguments (an object and an ImapProgressEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        void client_Progress(object sender, ImapProgressEventArgs e)
        {
            // Show progress information. 
            if (e.Length > 0 && e.State == ImapTransferState.Downloading)
            {
                Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage);
            }
        }
        
      6. Now you can connect to the IMAP server and show progress information while downloading or uploading messages in the Progress event handler.

      For VB.NET

      1. Create a new VB.NET project and add UltimateMail component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the IMAP component such as:
        ' Create a new Imap instance. 
        Dim client As New Imap()
        
        ' Connect to the IMAP server.
        client.Connect("myserver")
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up". For the purposes of this tutorial, the Imap.Progress event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        ' Register an event handler. 
        AddHandler client.Progress, AddressOf client_Progress
        
      5. Navigate to the reference listing for the Progress delegate. This listing will tell you what your event-handler method should look like. According to the EventHandler<ImapProgressEventArgs> delegate, your method must accept two arguments (an object and an ImapProgressEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        Private Sub client_Progress(ByVal sender As Object, ByVal e As ImapProgressEventArgs)
            ' Show progress information. 
            If e.Length > 0 AndAlso e.State = ImapTransferState.Downloading Then 
                Console.Write(vbCr & "Downloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage)
            End If 
        End Sub
        
      6. Now you can connect to the IMAP server and show progress information while downloading or uploading messages in the Progress event handler.