Sample code for 30+ languages & platforms
C

SMTP Inspection

See more SMTP Examples

Examine an SMTP session to view all communications sent to the SMTP server.

Chilkat C Downloads

C
#include <C_CkMailMan.h>
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailMan mailman;
    HCkEmail email;
    const char *mimeStr;

    success = FALSE;

    mailman = CkMailMan_Create();

    CkMailMan_putSmtpHost(mailman,"smtp.example.com");
    CkMailMan_putSmtpUsername(mailman,"myUsername");
    CkMailMan_putSmtpPassword(mailman,"myPassword");

    CkMailMan_putSmtpPort(mailman,465);
    CkMailMan_putStartTLS(mailman,TRUE);

    email = CkEmail_Create();

    CkEmail_putSubject(email,"This is a test");
    CkEmail_putBody(email,"This is a test");
    CkEmail_putFrom(email,"Alex <alex@example.com>");
    CkEmail_AddTo(email,"Christoph","christoph@example2.com");
    CkEmail_AddBcc(email,"Victor","victor@example3.com");

    //  Generate the MIME that is sent when SendEmail is called
    mimeStr = CkMailMan_renderToMime(mailman,email);
    printf("%s\n",mimeStr);

    success = CkMailMan_SendEmail(mailman,email);
    if (success == FALSE) {
        printf("%s\n",CkMailMan_lastErrorText(mailman));
        CkMailMan_Dispose(mailman);
        CkEmail_Dispose(email);
        return;
    }

    success = CkMailMan_CloseSmtpConnection(mailman);
    if (success != TRUE) {
        printf("Connection to SMTP server not closed cleanly.\n");
    }

    //  Examine the SMTP session log.
    printf("----\n");
    printf("Smtp Session Log:\n");
    printf("%s\n",CkMailMan_smtpSessionLog(mailman));

    //  ---------------------------------------------------
    //  Sample output:
    //  ---------------------------------------------------

    //  MIME-Version: 1.0
    //  Date: Fri, 06 Mar 2026 16:21:00 -0600
    //  Message-ID: <A5DC25990C93EC2676F4FBB2BB3BFA9EB34FF9D3@CHILKAT25>
    //  Content-Type: text/plain; charset=us-ascii; format=flowed
    //  Content-Transfer-Encoding: 7bit
    //  X-Priority: 3 (Normal)
    //  Subject: This is a test
    //  From: Alex <alex@example.com>
    //  To: Christoph <christoph@example2.com>
    //  Bcc: Victor <victor@example3.com>
    //  
    //  This is a test
    //  ----
    //  Smtp Session Log:
    //  220 smtp.example.com ESMTP Amazon WorkMail SMTP Service
    //  EHLO MYCOMPUTER<CRLF>
    //  250-smtp.example.com
    //  250-8BITMIME
    //  250-AUTH PLAIN LOGIN
    //  250 Ok
    //  AUTH LOGIN<CRLF>
    //  334 VXNlcm5hbWU6
    //  YWRtaW5AY2hpbGthdHNvZnQuY29t<CRLF>
    //  334 UGFzc3dvcmQ6
    //  {PasswordOrCredentials}
    //  235 Authentication successful.
    //  MAIL FROM:<alex@example.com><CRLF>
    //  250 Ok
    //  RCPT TO:<christoph@example2.com><CRLF>
    //  250 Ok
    //  RCPT TO:<victor@example3.com><CRLF>
    //  250 Ok
    //  DATA<CRLF>
    //  354 End data with <CR><LF>.<CR><LF>
    //  {388 bytes} <--- The MIME source of the email (examined via RenderToMime) was sent here.
    //  <CRLF>.<CRLF>
    //  250 Ok
    //  QUIT<CRLF>
    //  221 Bye


    CkMailMan_Dispose(mailman);
    CkEmail_Dispose(email);

    }