Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
email: HCkEmail;
filename: HCkStringBuilder;
mediaType: HCkStringBuilder;
found: Boolean;
i: Integer;
data: HCkBinData;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

email := CkEmail_Create();
success := CkEmail_LoadEml(email,'with-attachments.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
filename := CkStringBuilder_Create();
mediaType := CkStringBuilder_Create();
found := False;
i := 0;
while (i < CkEmail_getNumAttachments(email)) do
  begin
    CkStringBuilder_SetString(filename,CkEmail__getAttachmentFilename(email,i));
    CkStringBuilder_SetString(mediaType,CkEmail__getAttachmentContentType(email,i));
    if ((CkStringBuilder_ContentsEqual(filename,'agenda.csv',False) = True) and (CkStringBuilder_ContentsEqual(mediaType,'text/csv',False) = True) and (found = False)) then
      begin
        data := CkBinData_Create();
        success := CkEmail_GetAttachmentBd(email,i,data);
        if (success <> True) then
          begin
            Memo1.Lines.Add(CkEmail__lastErrorText(email));
            Exit;
          end;
        success := CkBinData_WriteFile(data,'received-agenda.csv');
        if (success <> True) then
          begin
            Memo1.Lines.Add('Failed: data.WriteFile.');
            Exit;
          end;
        Memo1.Lines.Add('Saved attachment: ' + IntToStr(CkBinData_getNumBytes(data)) + ' bytes.');
        found := True;
      end;
    i := i + 1;
  end;

if (found <> True) then
  begin
    Memo1.Lines.Add('No matching attachment.');
  end;
i := 0;
while (i < CkEmail_getNumRelatedItems(email)) do
  begin
    Memo1.Lines.Add('Related: ' + CkEmail__getRelatedContentType(email,i) + '  Content-ID: ' + CkEmail__getRelatedContentID(email,i));
    i := i + 1;
  end;

CkEmail_Dispose(email);
CkStringBuilder_Dispose(filename);
CkStringBuilder_Dispose(mediaType);
        CkBinData_Dispose(data);