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

Delphi DLL
var
success: Boolean;
email: HCkEmail;
data: HCkBinData;
image: HCkBinData;
cid: PWideChar;
html: HCkStringBuilder;

begin
success := False;

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

email := CkEmail_Create();
success := CkEmail_LoadEml(email,'text-and-html.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
data := CkBinData_Create();
success := CkBinData_AppendString(data,'Time,Activity' + #13#10 + '09:00,Welcome' + #13#10 + '10:00,Field session' + #13#10,'utf-8');
if (success <> True) then
  begin
    Memo1.Lines.Add('Failed: data.AppendString.');
    Exit;
  end;
success := CkEmail_AddAttachmentBd(email,'agenda.csv',data,'text/csv');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;

//  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
image := CkBinData_Create();
success := CkBinData_AppendEncoded(image,'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==','base64');
if (success <> True) then
  begin
    Memo1.Lines.Add('Failed: image.AppendEncoded.');
    Exit;
  end;
cid := CkEmail__addRelatedBd(email,'confirmed.png',image);
if (CkEmail_getLastMethodSuccess(email) <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
html := CkStringBuilder_Create();
CkStringBuilder_Append(html,'<p><img alt=''Confirmed'' src="cid:');
CkStringBuilder_Append(html,cid);
CkStringBuilder_Append(html,'"></p>');
CkStringBuilder_Append(html,CkEmail__getHtmlBody(email));
success := CkEmail_AddHtmlAlternativeBody(email,CkStringBuilder__getAsString(html));
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;

success := CkEmail_SaveEml(email,'with-attachments.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
Memo1.Lines.Add('Saved with-attachments.eml.');

CkEmail_Dispose(email);
CkBinData_Dispose(data);
CkBinData_Dispose(image);
CkStringBuilder_Dispose(html);