Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) Send HTML Email with Attachments

Demonstrates how to send an HTML email with file attachments.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkMailManW.h>
#include <C_CkEmailW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkMailManW mailman;
    HCkEmailW email;
    const wchar_t *contentIdStarfish;
    HCkStringBuilderW sbHtml;
    int numReplacements;
    BOOL success;
    const wchar_t *content;

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

    mailman = CkMailManW_Create();

    CkMailManW_putSmtpHost(mailman,L"smtp.my-starttls-mail-server.com");

    CkMailManW_putSmtpUsername(mailman,L"MY-SMTP-USERNAME");
    CkMailManW_putSmtpPassword(mailman,L"MY-SMTP-PASSWORD");

    CkMailManW_putStartTLS(mailman,TRUE);
    CkMailManW_putSmtpPort(mailman,587);

    // Create a new email object
    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Test SMTP API to Send HTML Email with Attachments");
    CkEmailW_putFrom(email,L"Joe Programmer <joe@my-starttls-mail-server.com>");
    CkEmailW_AddTo(email,L"Chilkat Support",L"support@chilkatsoft.com");

    // Add a plain-text alternative body, which will likely never be seen.
    // (It is shown if the receiving email client is incapable of displaying HTML email.)
    CkEmailW_AddPlainTextAlternativeBody(email,L"This is a plain-text alternative body...");

    // Our HTML will include an image, so add the related image here.
    contentIdStarfish = CkEmailW_addRelatedFile(email,L"qa_data/jpg/starfish.jpg");
    if (CkEmailW_getLastMethodSuccess(email) != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        return;
    }

    // The src attribute for the image tag is set to the contentIdStarfish:
    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHtml,L"<html><body><p>This is an HTML email with an embedded image.</p>");
    CkStringBuilderW_Append(sbHtml,L"<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
    numReplacements = CkStringBuilderW_Replace(sbHtml,L"CONTENT_ID_STARFISH",contentIdStarfish);

    CkEmailW_AddHtmlAlternativeBody(email,CkStringBuilderW_getAsString(sbHtml));

    // Finally, add some attachments to the email.
    // Add a file attachment.
    success = CkEmailW_AddFileAttachment2(email,L"qa_data/pdf/fishing.pdf",L"application/pdf");
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        CkStringBuilderW_Dispose(sbHtml);
        return;
    }

    // Add an attachment where the content is contained in a string.
    content = L"This is the content of the 2nd attached file.";
    CkEmailW_AddStringAttachment(email,L"someText.txt",content);

    // Send the HTML email.
    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        CkStringBuilderW_Dispose(sbHtml);
        return;
    }

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

    wprintf(L"HTML email with attachments sent!\n");


    CkMailManW_Dispose(mailman);
    CkEmailW_Dispose(email);
    CkStringBuilderW_Dispose(sbHtml);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.