Sample code for 30+ languages & platforms
Lua

Save Email Credentials in Apple Keychain or Windows Credentials Manager

See more Apple Keychain Examples

This example demonstrates how to save (or update) email credentials in the Apple Keychain.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local secrets = chilkat.Secrets.new()

-- On Windows, this is the Windows Credentials Manager
-- On MacOS/iOS, it is the Apple Keychain
secrets.location = "local_manager"

-- Specify the name of the secret.
-- service and username are required.
-- appName and domain are optional.
-- Note: The values are arbitrary and can be anything you want.
local json = chilkat.JsonObject.new()
json:update_string("appName", "MyEmailApp")
json:update_string("service", "SMTP")
json:update_string("domain", "example.com")
json:update_string("username", "joe@example.com")

-- Create or update the secret.
-- This secret is the SMTP password for the above email account.
local success = secrets:update_secret_str(json, "joes_password")
if not success then
    print(secrets.last_error_text)
    return
end

-- Let's save the IMAP secret for an account we'll read using IMAP..
json:update_string("appName", "MyEmailApp")
json:update_string("service", "IMAP")
json:update_string("domain", "example2.com")
json:update_string("username", "jane@example2.com")

success = secrets:update_secret_str(json, "janes_password")
if not success then
    print(secrets.last_error_text)
    return
end

print("Success.")