Sample code for 30+ languages & platforms
DataFlex

IMAP Login using SecureString Credentials

See more IMAP Examples

Demonstrates the Chilkat Imap.LoginSecure method, which authenticates a connected session using SecureString credentials. The first argument is the login name and the second is the password. This is the secure-string counterpart of Login; the mechanism is chosen by the AuthMethod property.

Background: A SecureString keeps the credential encrypted in memory and helps avoid leaving plaintext copies lying around in the process, which is why the password should come from an interactive prompt, an environment variable, or a secrets vault — never a hard-coded literal. For XOAUTH2, pass the login name in the first argument and the raw access token (without a Bearer prefix) in the second.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    String sPassword
    Variant vSecureLogin
    Handle hoSecureLogin
    Variant vSecurePassword
    Handle hoSecurePassword
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.LoginSecure method, which authenticates the session using SecureString
    //  credentials.  The 1st argument is the login name and the 2nd is the password.

    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

    //  The password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.

    //  Place the login name and password into SecureString objects.
    Get Create (RefClass(cComChilkatSecureString)) To hoSecureLogin
    If (Not(IsComObjectCreated(hoSecureLogin))) Begin
        Send CreateComObject of hoSecureLogin
    End
    Get ComAppend Of hoSecureLogin "user@example.com" To iSuccess
    Get Create (RefClass(cComChilkatSecureString)) To hoSecurePassword
    If (Not(IsComObjectCreated(hoSecurePassword))) Begin
        Send CreateComObject of hoSecurePassword
    End
    Get ComAppend Of hoSecurePassword sPassword To iSuccess

    Get pvComObject of hoSecureLogin to vSecureLogin
    Get pvComObject of hoSecurePassword to vSecurePassword
    Get ComLoginSecure Of hoImap vSecureLogin vSecurePassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Logged in securely."

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure