Delphi DLL
Delphi DLL
Process New Email by Scanning for Senders
Scan email and save application-selected emails to EML files with unique filenames.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, EmailBundle, Imap, Email, MessageSet;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
imap: HCkImap;
fetchUids: Boolean;
messageSet: HCkMessageSet;
bundle: HCkEmailBundle;
headersOnly: Boolean;
emailHeader: HCkEmail;
fullEmail: HCkEmail;
i: Integer;
numEmails: Integer;
uidStr: PWideChar;
uid: Integer;
filename: PWideChar;
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;
// We can choose to fetch UIDs or sequence numbers.
fetchUids := True;
// Fetch messages from the mailbox using a search criteria.
// This example finds NEW emails: these are emails that have the RECENT flag set, but not the SEEN flag:
messageSet := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'NEW',fetchUids,messageSet);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// This example will download headers, and then download
// the full email for those emails sent from a contact
// in our database.
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Also, attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
bundle := CkEmailBundle_Create();
headersOnly := True;
success := CkImap_FetchMsgSet(imap,headersOnly,messageSet,bundle);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Loop over the email objects...
emailHeader := CkEmail_Create();
fullEmail := CkEmail_Create();
i := 0;
numEmails := CkEmailBundle_getMessageCount(bundle);
while i < numEmails do
begin
CkEmailBundle_EmailAt(bundle,i,emailHeader);
// The sender's email address and name are available
// in the From, FromAddress, and FromName properties.
// If the sender is "Chilkat Support <support@chilkatsoft.com",
// then the From property will hold the entire string.
// the FromName property contains"Chilkat Support",
// and the FromAddress property contains "support@chilkatsoft.com"
Memo1.Lines.Add(CkEmail__from(emailHeader));
Memo1.Lines.Add(CkEmail__fromAddress(emailHeader));
Memo1.Lines.Add(CkEmail__fromName(emailHeader));
// Assume at this point your code checks to see if the sender
// is one in your contacts database. If so, this is
// the code you would write to download the entire
// email and save it to a file.
// The ckx-imap-uid header field is added when
// headers are downloaded. This makes it possible
// to get the UID from the email object.
uidStr := CkEmail__getHeaderField(emailHeader,'ckx-imap-uid');
uid := StrToInt(uidStr);
success := CkImap_FetchEmail(imap,False,uid,True,fullEmail);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// You can use the GenerateFilename method to
// generate a unique filename...
filename := CkEmail__generateFilename(fullEmail);
// SaveEml saves the entire email, including attachments.
success := CkEmail_SaveEml(fullEmail,filename);
Memo1.Lines.Add('--');
i := i + 1;
end;
// Disconnect from the IMAP server.
success := CkImap_Disconnect(imap);
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmailBundle_Dispose(bundle);
CkEmail_Dispose(emailHeader);
CkEmail_Dispose(fullEmail);
end;