Sample code for 30+ languages & platforms
DataFlex

Disconnect from an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Disconnect method, which closes the connection to the IMAP server. A failure indicates the connection could not be closed cleanly; the socket is nevertheless no longer intended for use. This example connects, logs in, and then disconnects.

Background: Disconnect closes the underlying TCP socket. It is lower-level than Logout, which first tells the server to end the IMAP session; the tidy shutdown is Logout followed by Disconnect. Always releasing connections matters because servers cap the number of simultaneous IMAP connections per account, and leaked connections can lock you out until they time out.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.Disconnect method, which closes the connection to the IMAP server.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  ... perform mailbox operations here ...

    //  Close the connection to the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Disconnected from the IMAP server."


End_Procedure