Sample code for 30+ languages & platforms
Delphi DLL

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mime: HCkMime;
bodyPart: HCkMime;

begin
success := False;

//  Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
//  part, and appends it.  The only argument is the file path; the content headers are chosen from
//  the filename extension.

mime := CkMime_Create();
success := CkMime_NewMultipartMixed(mime);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

//  A message body part.
bodyPart := CkMime_Create();
success := CkMime_SetBodyFromPlainText(bodyPart,'Please see the attached files.');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(bodyPart));
    Exit;
  end;
success := CkMime_AppendPart(mime,bodyPart);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

//  Attach files directly.
success := CkMime_AppendPartFromFile(mime,'qa_data/report.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;
success := CkMime_AppendPartFromFile(mime,'qa_data/photo.jpg');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

Memo1.Lines.Add('Number of parts: ' + IntToStr(CkMime_getNumParts(mime)));

CkMime_Dispose(mime);
CkMime_Dispose(bodyPart);