Sample code for 30+ languages & platforms
SQL Server

Use a PEM TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCertPem method, which sets the client-side certificate for SSL/TLS connections, loading it from PEM data or from a PEM file. The first argument may contain the PEM text itself or a path to a PEM file; the second is the PEM password. This example loads the client certificate from a PEM file.

Background: PEM is the familiar Base64 text format bracketed by -----BEGIN CERTIFICATE----- lines, and a single PEM can hold both a certificate and its (optionally encrypted) private key. It is the common format in Unix/OpenSSL environments, whereas PFX is more typical on Windows. Because this method accepts either the PEM text or a filename, you can supply credentials straight from a config value or secret store without writing them to disk.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MailMan.SetSslClientCertPem method, which sets the client-side
    --  certificate for SSL/TLS connections, loading it from PEM data or from a PEM file.  The
    --  1st argument may contain PEM text or a PEM file path; the 2nd is the PEM password.

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

    --  Configure the SMTP server connection.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
    EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword'

    --  Load the client certificate from a PEM file.
    EXEC sp_OAMethod @mailman, 'SetSslClientCertPem', @success OUT, 'qa_data/certs/client.pem', 'pem_password'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    EXEC sp_OAMethod @mailman, 'VerifySmtpLogin', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END


    PRINT 'Connected using a PEM TLS client certificate.'

    --  Note: The path "qa_data/certs/client.pem" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @mailman


END
GO