Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Delphi DLL Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Delphi DLL) 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 for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX DLL.

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Email, StringBuilder;

...

procedure TForm1.Button1Click(Sender: TObject);
var
email: HCkEmail;
success: Boolean;
sbHtml: HCkStringBuilder;
numReplaced: Integer;
sbBodyContent: HCkStringBuilder;
sbTemplate: HCkStringBuilder;
bCrlf: Boolean;
myHtmlReplyBody: PWideChar;

begin
email := CkEmail_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 := CkEmail_LoadEml(email,'qa_data/eml/receivedHtmlEmail.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;

//  If this email doesn't have an HTML body, then do something else...
//  (that's for another example maybe?)
if (CkEmail_HasHtmlBody(email) = False) then
  begin
    Memo1.Lines.Add('Email does not have an HTML body..');
    Exit;
  end;

//  Get the HTML body in a StringBuilder..
sbHtml := CkStringBuilder_Create();
CkStringBuilder_Append(sbHtml,CkEmail__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 := CkStringBuilder_Replace(sbHtml,'<BODY','<body');
numReplaced := CkStringBuilder_Replace(sbHtml,'</BODY','</body');

//  Get the HTML within the "body" open/close tags
sbBodyContent := CkStringBuilder_Create();
CkStringBuilder_Append(sbBodyContent,CkStringBuilder__getAfterBetween(sbHtml,'<body','>','</body>'));

//  Replace what we just extracted with a marker..
numReplaced := CkStringBuilder_Replace(sbHtml,CkStringBuilder__getAsString(sbBodyContent),'TO_BE_UPDATED');

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

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

//  Now for the original HTML body content:
numReplaced := CkStringBuilder_Replace(sbTemplate,'ORIGINAL_BODY_CONTENT',CkStringBuilder__getAsString(sbBodyContent));

//  Insert into sbHtml:
numReplaced := CkStringBuilder_Replace(sbHtml,'TO_BE_UPDATED',CkStringBuilder__getAsString(sbTemplate));

//  Use sbHtml as the new HTML body..
CkEmail_SetHtmlBody(email,CkStringBuilder__getAsString(sbHtml));

//  Examine the result HTML.
Memo1.Lines.Add(CkStringBuilder__getAsString(sbHtml));

//  Save the email.
CkEmail_SaveEml(email,'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.
// 

CkEmail_Dispose(email);
CkStringBuilder_Dispose(sbHtml);
CkStringBuilder_Dispose(sbBodyContent);
CkStringBuilder_Dispose(sbTemplate);

end;

 

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