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

Delphi ActiveX
var
success: Integer;
imap: TChilkatImap;
numMessages: Integer;
sbMime: TChilkatStringBuilder;
seqNum: Integer;

begin
success := 0;

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

imap := TChilkatImap.Create(Self);

//  Connect to an IMAP server.
//  Use TLS
imap.Ssl := 1;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

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

//  Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

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

sbMime := TChilkatStringBuilder.Create(Self);

for seqNum := 1 to numMessages do
  begin
    sbMime.Clear();
    success := imap.FetchSingleAsMimeSb(seqNum,0,sbMime.ControlInterface);
    if (success = 0) then
      begin
        Memo1.Lines.Add(imap.LastErrorText);
        Exit;
      end;

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

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