Delphi DLL
Delphi DLL
Iterate MIME Parts of an Email
See more Email Object Examples
Demonstrates 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.
Chilkat Delphi DLL Downloads
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
success: Boolean;
email: HCkEmail;
sbContentType: HCkStringBuilder;
caseSensitive: Boolean;
inlineOnly: Boolean;
excludeAttachments: Boolean;
searchSpec: PWideChar;
numParts: Integer;
i: Integer;
textBody: PWideChar;
attachedEmail: HCkEmail;
bdMime: HCkBinData;
bd: HCkBinData;
begin
success := False;
// 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;