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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
imap: HCkImap;
numMessages: Integer;
sbMime: HCkStringBuilder;
seqNum: Integer;

begin
success := False;

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

imap := CkImap_Create();

//  Connect to an IMAP server.
//  Use TLS
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;

//  Login
success := CkImap_Login(imap,'myLogin','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

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

//  The NumMessages property contains the number of messages in the selected mailbox.
numMessages := CkImap_getNumMessages(imap);
if (numMessages = 0) then
  begin
    Memo1.Lines.Add('No messages exist in the Inbox.');
    Exit;
  end;

sbMime := CkStringBuilder_Create();

for seqNum := 1 to numMessages do
  begin
    CkStringBuilder_Clear(sbMime);
    success := CkImap_FetchSingleAsMimeSb(imap,seqNum,False,sbMime);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkImap__lastErrorText(imap));
        Exit;
      end;

    //  The MIME source of the downloaded email is contained in sbMime.
  end;

//  Disconnect from the IMAP server.
success := CkImap_Disconnect(imap);

CkImap_Dispose(imap);
CkStringBuilder_Dispose(sbMime);