Sample code for 30+ languages & platforms
Delphi DLL

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
imap: HCkImap;
wantUids: Boolean;
msgSet: HCkMessageSet;
uid: Cardinal;
useUid: Boolean;
sbMime: HCkStringBuilder;

begin
success := False;

//  Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
//  StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
//  number, and the 3rd is the StringBuilder (cleared first).

imap := CkImap_Create();

CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);

success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
success := CkImap_Login(imap,'user@example.com','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

//  Search for the message ids to download, returning UIDs.
wantUids := True;
msgSet := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'ALL',wantUids,msgSet);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

if (CkMessageSet_getCount(msgSet) > 0) then
  begin
    uid := CkMessageSet_GetId(msgSet,0);

    //  Download the message's MIME into a StringBuilder.  The id is a UID.
    useUid := True;
    sbMime := CkStringBuilder_Create();
    success := CkImap_FetchSingleAsMimeSb(imap,uid,useUid,sbMime);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkImap__lastErrorText(imap));
        Exit;
      end;
    Memo1.Lines.Add('MIME length: ' + IntToStr(CkStringBuilder_getLength(sbMime)) + ' chars');
  end;

success := CkImap_Disconnect(imap);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

CkImap_Dispose(imap);
CkMessageSet_Dispose(msgSet);
    CkStringBuilder_Dispose(sbMime);