Link to home
Start Free TrialLog in
Avatar of CyberProgrammer
CyberProgrammer

asked on

Reading MS Outlook emails

Hi,

I need to know how I can read MS Outlook emails and Delete specific emails from them. I am using Delphi7

Thanks in advance for your time
and also:

to delete-manage mails

http://www.torry.net/apps/internet/email/EEMailer_027_Setup.exe

http://www.imibo.com/imidev/delphi/les/index.html

 and also
http://www.scalabium.com/faq/dc_tips.htm
#0117       To select a recipient from addressbook
#0120       To retrieve a folder list from MS Outlook
#0121       To retrieve items (messages/tasks/etc) from any Outllok folder
#0123       To retrieve and save the attachments from MS Outllok message

Hope this help
Avatar of CyberProgrammer
CyberProgrammer

ASKER

bernani,

Thanks a lot for your help and time.

The link you provided me showed me how to manipulate MS Outlook which is great :) But I couldn't find something to show me how I can programmatically delete a specific email from the MS Outlook emails list....so any ideas?

Thanks in advance for your time
Hi,

another great page

http://www.djpate.freeserve.co.uk/AutoOutl.htm

To answer your last question, I'm not sure it's possible to delete mails directly from Outlook via Delphi.

I'll check and come back later (evetually with sample code showing how to do).




Hi,

Here is a way to delete a mail:

procedure TForm1.Button3Click(Sender: TObject);
const
// in his example, I delete a mail called 'Test-001' which is in the FolderInbox
// declare a constant for it (others to be found in Word97/2000 ....pas unit)
olFolderInbox = $00000006;

var
Outlook, NameSpace, FIS, FI: OleVariant;
i: Integer;

begin
Outlook := CreateOleObject('Outlook.Application');
NameSpace := outlook.GetNameSpace('MAPI');
FI := NameSpace.GetDefaultFolder(olFolderInbox);  
   For i := FI.Items.Count downto 1 do                    
   begin
   FIS := FI.Items.Item(i);                                      
   Richedit1.lines.Add('Item number: ' + inttostr(i));          //item number
   Richedit1.lines.add(FIS.subject + #10#13 + FIS.Body); // add the subject and the body in a richedit
                                                                                  // only here to show the code is working
   Richedit2.lines.add(FIS.subject);                                 // only the mail subject in a secondRichedit
      if vartostr(fis) = 'Test-001' then                              // see if it equals ouw item to be deleted
      begin
      Showmessage('Sucess - found at position ' + inttostr(i));  // confirm before deleting
      FI.Items.Item(i).delete;                                                  // bye  
      end;
   end;
Outlook := UnAssigned;                    
end;

Tested and working. If you open Oulook, you will find the deleted item in the trash.

Hope this help.

To be coplete, I Forgot to say that you need to add:

uses
ComObj; {for CreateOleObject }



ASKER CERTIFIED SOLUTION
Avatar of bernani
bernani
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Sorry ... typo .....

_____ Cut and save as Oulook1.dfm____________

needs to be

_____ Cut and save as OulookMail1.dfm____________
Hi,

Thank you SO much for the sources, it really helped me a lot understand.

Although whenever I try to run the application I always get ('Invalid Class String')  then if I pressed F9 again it run, then if I pressed any of the three buttons on the screen I get error ('Invalid Varient Operation') then it stop/ crash at this line:


var
i: Integer;
begin
FI := NameSpace.GetDefaultFolder(olFolderInbox);


So any idea why I am getting those two errors on run?

Thanks in advance for your time and help

Hi,

"invalid class string" generally means that the COM server for the progid isn't registered on the machine and the second error you mention is the consequence of the first (if the class string isn't valid, each operation involving it will be logically invalid).

Note that this code is written to be used with MSOutlook2000 on XPPro-SP2 (re-installed to check the code above - I personnaly don't use MS products as email-reader). As the code above don't display the MS logon dialog box at startup, all the config parameters for accessing the mail-acount of MSOutlook need to be valid (valid account login and password with auto-saved checked).

Try to write a brand new project to connect to your MS-Outlook version with the minimal declarations:

// create an instance of Outlook
Outlook := CreateOleObject('Outlook.Application');

//defines a MAPI's namespace which have a collection of folders
NameSpace := outlook.GetNameSpace('MAPI');

// get the FolderInbox (an item of the collection of folders)
// in Outlook97/2000 the name of each Outlook folder of the collection
//has a const identifier, for example: olFolderInbox = $00000006;
FI := NameSpace.GetDefaultFolder(olFolderInbox);  

// count the number of items present in the FolderInbox
showmessage (inttostr(FI.Items.Count));

Outlook := UnAssigned;

Don't forget to add in your uses clause the ComObj unit and the declaration of each var and const.

Compile and see the result. If it's OK, you need to have a Msgbox displaying the number of items present in your FolderInbox.  

Eventually:
 
Check also in your MSOutlook-Version.pas unit which are the constants defined for each folders (  FolderDeletedItems, FolderOutbox, lFolderSentMail, FolderInbox .....).

Check also that you can access your inbox mail without the need of supplying a login and password.

And remember that in a real application, you should check if MSOutlook is installed, the version, embed the CreateOleObject in  try .... finally / try ... except block, ....., add a login screen, ....

Good luck.