Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
imap: TChilkatImap;
matches: TMessageSet;
uid: Cardinal;
email: TChilkatEmail;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
imap := TChilkatImap.Create(Self);
imap.Ssl := 1;
imap.Port := 993;
imap.RequireSslCertVerify := 1;
imap.PeekMode := 1;
imap.AutoDownloadAttachments := 1;
success := imap.Connect('imap.example.com');
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
success := imap.Login('MAIL_USER','MAIL_PASSWORD');
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
success := imap.SelectMailbox('INBOX');
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
matches := TMessageSet.Create(Self);
success := imap.QueryMbx('SUBJECT "Sample message"',1,matches.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
if (matches.Count = 0) then
begin
Memo1.Lines.Add('No matching message in INBOX.');
success := imap.Disconnect();
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
Exit;
end;
// The search returned UIDs. Use the same identifier type for the fetch.
uid := matches.GetId(0);
email := TChilkatEmail.Create(Self);
success := imap.FetchEmail(0,uid,1,email.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Mailbox INBOX, UIDVALIDITY ' + IntToStr(imap.UidValidity) + ', UID ' + IntToStr(uid));
success := imap.Disconnect();
if (success <> 1) then
begin
Memo1.Lines.Add(imap.LastErrorText);
Exit;
end;
// The downloaded Email remains usable after disconnecting.
Memo1.Lines.Add('Subject: ' + email.Subject + ' Files: ' + IntToStr(email.NumAttachments));
success := email.SaveEml('imap-received.eml');
if (success <> 1) then
begin
Memo1.Lines.Add(email.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Saved imap-received.eml.');