Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get IMAP UID from Email Header
See more IMAP Examples
Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.EmailBundle,
Chilkat.Imap,
Chilkat.Email,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
fetchUids: Boolean;
messageSet: TMessageSet;
bundle: TEmailBundle;
headersOnly: Boolean;
email: TEmail;
msgSet1: TMessageSet;
i: Integer;
szBundle: Integer;
uidStr: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := TImap.Create;
// Connect to an IMAP server.
// Use TLS
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Login
success := imap.Login('login','password');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
fetchUids := True;
// Get the message UIDs of all the emails in the mailbox
messageSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',fetchUids,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
bundle := TEmailBundle.Create;
headersOnly := True;
success := imap.FetchMsgSet(headersOnly,messageSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// The UID of each fetched email header is available
// in the ckx-imap-uid header field.
email := TEmail.Create;
msgSet1 := TMessageSet.Create;
i := 0;
szBundle := bundle.MessageCount;
while i < szBundle do
begin
bundle.EmailAt(i,email);
// Build a message set containing one UID
uidStr := email.GetHeaderField('ckx-imap-uid');
msgSet1.FromCompactString(uidStr);
// Move this message to some other folder.
success := imap.MoveMessages(msgSet1,'someOtherFolder');
if (success <> True) then
begin
WriteLn(imap.LastErrorText);
// Exit the loop...
i := szBundle;
end;
i := i + 1;
end;
// Disconnect from the IMAP server.
success := imap.Disconnect();
imap.Free;
messageSet.Free;
bundle.Free;
email.Free;
msgSet1.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.