Sample code for 30+ languages & platforms
Delphi ActiveX 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 ActiveX Downloads

Delphi ActiveX
var
success: Integer;
email: TChilkatEmail;
filename: TChilkatStringBuilder;
mediaType: TChilkatStringBuilder;
found: Integer;
i: Integer;
data: TChilkatBinData;

begin
success := 0;

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

email := TChilkatEmail.Create(Self);
success := email.LoadEml('with-attachments.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
filename := TChilkatStringBuilder.Create(Self);
mediaType := TChilkatStringBuilder.Create(Self);
found := 0;
i := 0;
while (i < email.NumAttachments) do
  begin
    filename.SetString(email.GetAttachmentFilename(i));
    mediaType.SetString(email.GetAttachmentContentType(i));
    if ((filename.ContentsEqual('agenda.csv',0) = 1) and (mediaType.ContentsEqual('text/csv',0) = 1) and (found = 0)) then
      begin
        data := TChilkatBinData.Create(Self);
        success := email.GetAttachmentBd(i,data.ControlInterface);
        if (success <> 1) then
          begin
            Memo1.Lines.Add(email.LastErrorText);
            Exit;
          end;
        success := data.WriteFile('received-agenda.csv');
        if (success <> 1) then
          begin
            Memo1.Lines.Add('Failed: data.WriteFile.');
            Exit;
          end;
        Memo1.Lines.Add('Saved attachment: ' + IntToStr(data.NumBytes) + ' bytes.');
        found := 1;
      end;
    i := i + 1;
  end;

if (found <> 1) then
  begin
    Memo1.Lines.Add('No matching attachment.');
  end;
i := 0;
while (i < email.NumRelatedItems) do
  begin
    Memo1.Lines.Add('Related: ' + email.GetRelatedContentType(i) + '  Content-ID: ' + email.GetRelatedContentID(i));
    i := i + 1;
  end;