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

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
email: TChilkatEmail;
data: TChilkatBinData;
image: TChilkatBinData;
cid: WideString;
html: TChilkatStringBuilder;

begin
success := 0;

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

email := TChilkatEmail.Create(Self);
success := email.LoadEml('text-and-html.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
data := TChilkatBinData.Create(Self);
success := data.AppendString('Time,Activity' + #13#10 + '09:00,Welcome' + #13#10 + '10:00,Field session' + #13#10,'utf-8');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Failed: data.AppendString.');
    Exit;
  end;
success := email.AddAttachmentBd('agenda.csv',data.ControlInterface,'text/csv');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;

//  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
image := TChilkatBinData.Create(Self);
success := image.AppendEncoded('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==','base64');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Failed: image.AppendEncoded.');
    Exit;
  end;
cid := email.AddRelatedBd('confirmed.png',image.ControlInterface);
if (email.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
html := TChilkatStringBuilder.Create(Self);
html.Append('<p><img alt=''Confirmed'' src="cid:');
html.Append(cid);
html.Append('"></p>');
html.Append(email.GetHtmlBody());
success := email.AddHtmlAlternativeBody(html.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;

success := email.SaveEml('with-attachments.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Saved with-attachments.eml.');