Sample code for 30+ languages & platforms
Delphi DLL

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mime: HCkMime;
textPart: HCkMime;
htmlPart: HCkMime;

begin
success := False;

//  Demonstrates the Mime.NewMultipartAlternative method, which initializes the object as an empty
//  multipart/alternative entity, clearing any existing content.  It takes no arguments.

mime := CkMime_Create();

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

//  multipart/alternative holds two representations of the SAME content; the client displays the
//  last one it can render.  Add a plain-text part, then an HTML part.
textPart := CkMime_Create();
success := CkMime_SetBodyFromPlainText(textPart,'Hello (plain text version).');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(textPart));
    Exit;
  end;
success := CkMime_AppendPart(mime,textPart);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

htmlPart := CkMime_Create();
success := CkMime_SetBodyFromHtml(htmlPart,'<html><body><b>Hello</b> (HTML version).</body></html>');
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(htmlPart));
    Exit;
  end;
success := CkMime_AppendPart(mime,htmlPart);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMime__lastErrorText(mime));
    Exit;
  end;

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

CkMime_Dispose(mime);
CkMime_Dispose(textPart);
CkMime_Dispose(htmlPart);