Sample code for 30+ languages & platforms
Delphi DLL

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
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Imap, StringBuilder;

...

procedure TForm1.Button1Click(Sender: TObject);
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);

end;