Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
Read Mail HeadersDelphi sample code to read mail headers for all undeleted emails in an IMAP mailbox and display the From, Subject, and Attachment filenames and sizes. procedure TForm1.Button3Click(Sender: TObject);
var
connectSuccess: Integer;
loginSuccess: Integer;
selectSuccess: Integer;
mset: IMessageSet;
n: Integer;
i: Integer;
email: IChilkatEmail2;
bundle: IChilkatEmailBundle2;
uid: string;
numAttach: Integer;
j: Integer;
begin
loginSuccess := 0;
selectSuccess := 0;
// Import the Chilkat IMAP ActiveX and drop an instance
// of ChilkatImap onto your Delphi program's form.
ChilkatImap1.UnlockComponent('anything for 30-day trial');
// -------------------------
// Connect to an IMAP server.
// -------------------------
connectSuccess := ChilkatImap1.Connect('my-Imap-Server-Hostname');
if (connectSuccess = 0) then
begin
ShowMessage('Failed to connect!');
ChilkatImap1.SaveLastError('connectErrorLog.txt');
end;
// -------------------------
// IMAP Login
// -------------------------
if (connectSuccess = 1) then
loginSuccess := ChilkatImap1.Login('myLogin', 'myPassword');
if (loginSuccess = 0) then
begin
ShowMessage('Failed to login!');
ChilkatImap1.SaveLastError('loginErrorLog.txt');
end;
// -------------------------
// Select an IMAP Mailbox
// -------------------------
if (loginSuccess = 1) then
selectSuccess := ChilkatImap1.SelectMailbox('INBOX');
if (selectSuccess = 0) then
begin
ShowMessage('Failed to select mailbox!');
ChilkatImap1.SaveLastError('selectErrorLog.txt');
end;
if (selectSuccess = 1) then
begin
// -------------------------
// Find all undeleted emails
// -------------------------
mset := ChilkatImap1.Search('UNDELETED',1);
if (mset = nil) then
begin
ShowMessage('Failed to fetch Uids');
ChilkatImap1.SaveLastError('getUidsErrorLog.txt');
end
else
begin
// Fetch the headers for all messages in the bundle.
bundle := ChilkatImap1.FetchHeaders(mset);
// If bundle = nil, an error occurred. You would then check
// the last-error information.
// For each UID, fetch the email and display the FROM and Subject
n := bundle.MessageCount;
for i := 0 to n-1 do
begin
email := bundle.GetEmail(i);
// Get and display the IMAP UID...
uid := email.GetHeaderField('ckx-imap-uid');
Memo1.Lines.Add(uid);
numAttach := ChilkatImap1.GetMailNumAttach(email);
Memo1.Lines.Add('Number of attachments = ' + IntToStr(numAttach));
for j := 0 to numAttach-1 do
Memo1.Lines.Add(' ' + ChilkatImap1.GetMailAttachFilename(email,j) +
' ' + IntToStr(ChilkatImap1.GetMailAttachSize(email,j)) + ' bytes');
Memo1.Lines.Add(email.From);
Memo1.Lines.Add(email.Subject);
Memo1.Lines.Add('----');
end;
end;
end;
ShowMessage('Finished!');
end;
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.