PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSecureString.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; 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.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; The password is not hard-coded in source. Insert code here to obtain it from an
; interactive prompt, environment variable, or a secrets vault.
password.s
; Place the login name and password into SecureString objects.
secureLogin.i = CkSecureString::ckCreate()
If secureLogin.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSecureString::ckAppend(secureLogin,"user@example.com")
securePassword.i = CkSecureString::ckCreate()
If securePassword.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSecureString::ckAppend(securePassword,password)
success = CkImap::ckLoginSecure(imap,secureLogin,securePassword)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSecureString::ckDispose(secureLogin)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndIf
Debug "Logged in securely."
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSecureString::ckDispose(secureLogin)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkSecureString::ckDispose(secureLogin)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndProcedure