Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @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.

    DECLARE @imap int
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @imap, 'Ssl', 1
    EXEC sp_OASetProperty @imap, 'Port', 993

    EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.example.com'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        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.
    DECLARE @password nvarchar(4000)

    --  Place the login name and password into SecureString objects.
    DECLARE @secureLogin int
    EXEC @hr = sp_OACreate 'Chilkat.SecureString', @secureLogin OUT

    EXEC sp_OAMethod @secureLogin, 'Append', @success OUT, 'user@example.com'
    DECLARE @securePassword int
    EXEC @hr = sp_OACreate 'Chilkat.SecureString', @securePassword OUT

    EXEC sp_OAMethod @securePassword, 'Append', @success OUT, @password

    EXEC sp_OAMethod @imap, 'LoginSecure', @success OUT, @secureLogin, @securePassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @secureLogin
        EXEC @hr = sp_OADestroy @securePassword
        RETURN
      END

    PRINT 'Logged in securely.'

    EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @secureLogin
        EXEC @hr = sp_OADestroy @securePassword
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @secureLogin
    EXEC @hr = sp_OADestroy @securePassword


END
GO