Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 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.

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  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.
set secureLogin [new_CkSecureString]

CkSecureString_Append $secureLogin "user@example.com"
set securePassword [new_CkSecureString]

CkSecureString_Append $securePassword $password

set success [CkImap_LoginSecure $imap $secureLogin $securePassword]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecureString $secureLogin
    delete_CkSecureString $securePassword
    exit
}

puts "Logged in securely."

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkSecureString $secureLogin
    delete_CkSecureString $securePassword
    exit
}


delete_CkImap $imap
delete_CkSecureString $secureLogin
delete_CkSecureString $securePassword