Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Create HTML Email ReplyThe 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..
#include <CkEmailW.h> #include <CkStringBuilderW.h> void ChilkatSample(void) { CkEmailW email; // 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. bool success = email.LoadEml(L"qa_data/eml/receivedHtmlEmail.eml"); if (success != true) { wprintf(L"%s\n",email.lastErrorText()); return; } // If this email doesn't have an HTML body, then do something else... // (that's for another example maybe?) if (email.HasHtmlBody() == false) { wprintf(L"Email does not have an HTML body..\n"); return; } // Get the HTML body in a StringBuilder.. CkStringBuilderW sbHtml; sbHtml.Append(email.getHtmlBody()); // 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. int numReplaced = sbHtml.Replace(L"<BODY",L"<body"); numReplaced = sbHtml.Replace(L"</BODY",L"</body"); // Get the HTML within the "body" open/close tags CkStringBuilderW sbBodyContent; sbBodyContent.Append(sbHtml.getAfterBetween(L"<body",L">",L"</body>")); // Replace what we just extracted with a marker.. numReplaced = sbHtml.Replace(sbBodyContent.getAsString(),L"TO_BE_UPDATED"); // Create a template for the new body: CkStringBuilderW sbTemplate; bool bCrlf = true; sbTemplate.AppendLine(L"REPLY_HTML_CONTENT",bCrlf); sbTemplate.AppendLine(L"<br>",bCrlf); sbTemplate.AppendLine(L"<div class=\"moz-signature\">Best Regards,<br>",bCrlf); sbTemplate.AppendLine(L" John Smith<br>",bCrlf); sbTemplate.AppendLine(L" Chilkat Software, Inc.<br>",bCrlf); sbTemplate.AppendLine(L" <p>",bCrlf); sbTemplate.AppendLine(L" <a href=\"https://twitter.com/chilkatsoft\">Follow Chilkat on",bCrlf); sbTemplate.AppendLine(L" Twitter</a>",bCrlf); sbTemplate.AppendLine(L" </p>",bCrlf); sbTemplate.AppendLine(L"</div>",bCrlf); sbTemplate.AppendLine(L"<div class=\"moz-cite-prefix\">On 2/8/2017 4:25 PM, Chilkat Support",bCrlf); sbTemplate.AppendLine(L" wrote:<br>",bCrlf); sbTemplate.AppendLine(L"</div>",bCrlf); sbTemplate.AppendLine(L"<blockquote>",bCrlf); sbTemplate.AppendLine(L"ORIGINAL_BODY_CONTENT",bCrlf); sbTemplate.AppendLine(L"</blockquote>",bCrlf); sbTemplate.AppendLine(L"<br>",bCrlf); // Replace the markers in the template with actual HTML. // First our reply HTML: const wchar_t *myHtmlReplyBody = L"This is my <font color=\"#6600cc\"><b>reply HTML</b></font><br>"; numReplaced = sbTemplate.Replace(L"REPLY_HTML_CONTENT",myHtmlReplyBody); // Now for the original HTML body content: numReplaced = sbTemplate.Replace(L"ORIGINAL_BODY_CONTENT",sbBodyContent.getAsString()); // Insert into sbHtml: numReplaced = sbHtml.Replace(L"TO_BE_UPDATED",sbTemplate.getAsString()); // Use sbHtml as the new HTML body.. email.SetHtmlBody(sbHtml.getAsString()); // Examine the result HTML. wprintf(L"%s\n",sbHtml.getAsString()); // Save the email. email.SaveEml(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. // } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.