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
(Delphi DLL) Iterate MIME Parts of an EmailDemonstrates how to iterate over the MIME sub-parts of an email, and retrieve the content of each MIME sub-part body. Note: This example requires some new features added to Chilkat v9.5.0.95.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, BinData, Email; ... procedure TForm1.Button1Click(Sender: TObject); var email: HCkEmail; success: Boolean; sbContentType: HCkStringBuilder; caseSensitive: Boolean; inlineOnly: Boolean; excludeAttachments: Boolean; searchSpec: PWideChar; numParts: Integer; i: Integer; textBody: PWideChar; attachedEmail: HCkEmail; bdMime: HCkBinData; bd: HCkBinData; begin // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // See the following Chilkat post to Quickly Understand Email MIME email := CkEmail_Create(); success := CkEmail_LoadEml(email,'qa_data/eml/sample.eml'); if (success = False) then begin Memo1.Lines.Add('Failed to load .eml'); Exit; end; sbContentType := CkStringBuilder_Create(); caseSensitive := False; // Get the total number of non-multipart MIME sub-parts. // (This is a simple way of iterating over all the MIME leaf parts regardless of the MIME tree structure) inlineOnly := False; excludeAttachments := False; searchSpec := '*/*'; numParts := CkEmail_GetNumPartsOfType(email,searchSpec,inlineOnly,excludeAttachments); i := 0; while i < numParts do begin // What is the Content-Type of this MIME part? CkStringBuilder_Append(sbContentType,CkEmail__getNthContentType(email,i,searchSpec,inlineOnly,excludeAttachments)); if (CkStringBuilder_StartsWith(sbContentType,'text/',caseSensitive) = True) then begin // Get the text body of this MIME part. textBody := CkEmail__getNthTextPartOfType(email,i,searchSpec,inlineOnly,excludeAttachments); Memo1.Lines.Add('Got text body for ' + CkStringBuilder__getAsString(sbContentType)); end else begin if (CkStringBuilder_ContentsEqual(sbContentType,'message/rfc822',caseSensitive) = True) then begin // If the Content-Type is message/rfc822, then the MIME body for this part contains a full embedded MIME messages. // Your application could load it into a Chilkat email object and recursively process... attachedEmail := CkEmail_Create(); bdMime := CkBinData_Create(); CkEmail_GetNthBinaryPartOfTypeBd(email,i,searchSpec,inlineOnly,excludeAttachments,bdMime); CkEmail_SetFromMimeBd(attachedEmail,bdMime); // Now your app can recursively process the attachedEmail... end else begin // Get the bytes of this MIME body part. bd := CkBinData_Create(); CkEmail_GetNthBinaryPartOfTypeBd(email,i,searchSpec,inlineOnly,excludeAttachments,bd); Memo1.Lines.Add('Got binary body for ' + CkStringBuilder__getAsString(sbContentType) + ' numBytes = ' + IntToStr(CkBinData_getNumBytes(bd))); end; end; CkStringBuilder_Clear(sbContentType); i := i + 1; end; CkEmail_Dispose(email); CkStringBuilder_Dispose(sbContentType); CkEmail_Dispose(attachedEmail); CkBinData_Dispose(bdMime); CkBinData_Dispose(bd); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.