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
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) Create HTML Email Reply

The goal of this example is to create a new HTML body where the new reply HTML is at the top, and the original HTML body is below.

The .eml files for this example are available at HTML Reply Email Samples (.eml)

Note: This is only one of a million ways to do it..

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_CkEmailW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkEmailW email;
    BOOL success;
    HCkStringBuilderW sbHtml;
    int numReplaced;
    HCkStringBuilderW sbBodyContent;
    HCkStringBuilderW sbTemplate;
    BOOL bCrlf;
    const wchar_t *myHtmlReplyBody;

    email = CkEmailW_Create();

    // To create this example, I used Mozilla Thunderbird (with a GMail accont)
    // to first send a simple HTML email to my chilkatsoft.com email address.  I then 
    // used Thunderbird to reply with more HTML.
    // I then saved both emails as .eml files.  These are available at
    // http://chilkatdownload.com/example_data/htmlreplyemail.zip

    // Load the .eml for the 1st email that was received. This is the original
    // HTML email before the reply HTML is added.
    success = CkEmailW_LoadEml(email,L"qa_data/eml/receivedHtmlEmail.eml");
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return;
    }

    // If this email doesn't have an HTML body, then do something else...
    // (that's for another example maybe?)
    if (CkEmailW_HasHtmlBody(email) == FALSE) {
        wprintf(L"Email does not have an HTML body..\n");
        CkEmailW_Dispose(email);
        return;
    }

    // Get the HTML body in a StringBuilder..
    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHtml,CkEmailW_getHtmlBody(email));

    // The HTML body contains this:
    // <html><head>
    // <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    //   </head>
    //   <body bgcolor="#FFFFFF" text="#000000">
    //     <font face="Calibri"><br>
    //       This is a test <font color="#3366ff"><i>HTML email</i></font>...<br>
    //       <br>
    //       <br>
    //     </font>
    //   </body>
    // </html>

    // Make sure the BODY tags are lowercase.
    numReplaced = CkStringBuilderW_Replace(sbHtml,L"<BODY",L"<body");
    numReplaced = CkStringBuilderW_Replace(sbHtml,L"</BODY",L"</body");

    // Get the HTML within the "body" open/close tags
    sbBodyContent = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbBodyContent,CkStringBuilderW_getAfterBetween(sbHtml,L"<body",L">",L"</body>"));

    // Replace what we just extracted with a marker..
    numReplaced = CkStringBuilderW_Replace(sbHtml,CkStringBuilderW_getAsString(sbBodyContent),L"TO_BE_UPDATED");

    // Create a template for the new body:
    sbTemplate = CkStringBuilderW_Create();
    bCrlf = TRUE;
    CkStringBuilderW_AppendLine(sbTemplate,L"REPLY_HTML_CONTENT",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"<br>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"<div class=\"moz-signature\">Best Regards,<br>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"  John Smith<br>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"  Chilkat Software, Inc.<br>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"  <p>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"    <a href=\"https://twitter.com/chilkatsoft\">Follow Chilkat on",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"      Twitter</a>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"  </p>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"</div>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"<div class=\"moz-cite-prefix\">On 2/8/2017 4:25 PM, Chilkat Support",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"  wrote:<br>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"</div>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"<blockquote>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"ORIGINAL_BODY_CONTENT",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"</blockquote>",bCrlf);
    CkStringBuilderW_AppendLine(sbTemplate,L"<br>",bCrlf);

    // Replace the markers in the template with actual HTML.
    // First our reply HTML:
    myHtmlReplyBody = L"This is my <font color=\"#6600cc\"><b>reply HTML</b></font><br>";
    numReplaced = CkStringBuilderW_Replace(sbTemplate,L"REPLY_HTML_CONTENT",myHtmlReplyBody);

    // Now for the original HTML body content:
    numReplaced = CkStringBuilderW_Replace(sbTemplate,L"ORIGINAL_BODY_CONTENT",CkStringBuilderW_getAsString(sbBodyContent));

    // Insert into sbHtml:
    numReplaced = CkStringBuilderW_Replace(sbHtml,L"TO_BE_UPDATED",CkStringBuilderW_getAsString(sbTemplate));

    // Use sbHtml as the new HTML body..
    CkEmailW_SetHtmlBody(email,CkStringBuilderW_getAsString(sbHtml));

    // Examine the result HTML.
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbHtml));

    // Save the email.
    CkEmailW_SaveEml(email,L"qa_output/emailWithUpdatedHtmlBody.eml");

    // Note: To finish creating the reply email, modify the To/From/CC headers,
    // the subject, etc.  You may also with to iterate over the headers (from 1 to NumHeaderFields)
    // and selectively remove all except the particular headers you wish to keep.
    // You should keep: MIME-Version, Content-Type, Message-ID.  Chilkat will automaticaly
    // generate and replace the Message-ID at the time of sending.  You'll want to replace
    // the Date header with the current date/time.
    // 


    CkEmailW_Dispose(email);
    CkStringBuilderW_Dispose(sbHtml);
    CkStringBuilderW_Dispose(sbBodyContent);
    CkStringBuilderW_Dispose(sbTemplate);

    }

 

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