Sample code for 30+ languages & platforms
SQL Server

Get JSON Details from the Last MailMan Call

See more SMTP Examples

Demonstrates the Chilkat MailMan.GetLastJsonData method, which copies any structured JSON details produced by the immediately preceding method call into a JsonObject. Not every MailMan method produces JSON details, and the method must be called immediately after the call of interest. This example sends an email and then retrieves the JSON details, which include the negotiated TLS version and parameters, the SMTP server's last response and status code, and the result of SMTP authentication.

Tip: JSON parsing code for this result can be generated at Chilkat Tools.

Background: Some Chilkat methods gather structured diagnostic information alongside their normal result — details that are far more useful as data than as a log string. GetLastJsonData exposes that as a JsonObject you can query programmatically, making it easy to record exactly which TLS version was negotiated or which SMTP status code came back. Because it reflects only the most recent call, retrieve it right away: any subsequent MailMan method may replace it.

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.GetLastJsonData method, which copies any structured JSON details
    --  produced by the immediately preceding method call into a JsonObject.  Not every MailMan
    --  method produces JSON details, and it must be called immediately after the method of
    --  interest.

    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'

    --  Send an email.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Test email'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'Hello!'
    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Immediately retrieve any JSON details produced by the send.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @mailman, 'GetLastJsonData', NULL, @json

    --  Emit non-compact (indented) JSON for readability.
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  Sample output:
    --  
    --    {
    --      "tls": {
    --        "params": {
    --          "sniHostname": "mail.chilkat.io",
    --          "allowConnectionOnlyIfServerChooses": "SSL 3.0 or higher"
    --        },
    --        "negotiatedTlsVersion": "TLS 1.3"
    --      },
    --      "smtp": {
    --        "lastResponse": "250 OK id=1wkjeT-00000002yb4-0uve",
    --        "lastStatus": 250
    --      },
    --      "smtpAuth": {
    --        "user": "test@chilkat.io",
    --        "method": "login",
    --        "statusCode": 235,
    --        "success": true
    --      }
    --    }
    --  
    --  JSON parsing code for this result can be generated at Chilkat's online tool:
    --  https://tools.chilkat.io/jsonParse

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @json


END
GO