Board index » delphi » Newbie question : Outlook 2000, getting info from the InBox folder

Newbie question : Outlook 2000, getting info from the InBox folder

Hi,

I'm trying to write a procedure wich sequentialy scans the InBox
folder, and displays the Subject en Senders E-Mail address.
For this e-mail address, I'm using CDO for resolving it.
Everything runs fine intil the end of the loop, when Delphi
raises an exception.
Any ideas? Thanks.

Function GetAddress(Nr : Integer) : String;
var
  OA, FO, NS, MI, CDOMsg, CDOSession : OleVariant;
begin
  Result:='';

  OA := CreateOleObject('Outlook.Application');
  NS := OA.GetNameSpace('MAPI');
  FO := NS.GetDefaultFolder(olFolderInBox);
  MI := FO.Items.Item(Nr); //get the first mailitem
  CDOSession := CreateOleObject('Mapi.Session');
  CDOSession.Logon('','', FALSE, FALSE);
  Try
    CDOMsg := CDOSession.GetMessage(MI.EntryID, MI.Parent.StoreID);
    Result:=CDOMsg.Sender.Address;
//    ShowMessage(CDOMsg.Sender.Address);
  finally
    CDOSession.Logoff;
    CDOSession:=unassigned;
    FO:=Unassigned;
    OA:=Unassigned;
  end;
end;

procedure TMainForm.Button1Click(Sender: TObject);
var
  app: OutlookApplication;
  ns: NameSpace;
  tf, mf: MAPIFolder;
  i: Items;
  j: integer;
  ti, Reply : MailItem;
  s: string;
  address : String;
begin
  Memo1.Clear;
  app := CoOutlookApplication.Create;
  ns := app.GetNameSpace('MAPI');
  tf := ns.GetDefaultFolder(olFolderInBox); //olFolderTasks);
  i := tf.Items;

  for j := 1 to i.Count do
  begin
    Try
      ti := i.Item(j) as MailItem; //TaskItem;

      Address:=GetAddress(j);

      s := ti.SenderName+';'+Address+';'+ti.Subject;

      Memo1.Lines.Add(s);
    Except
    End;
  end;
end;

 

Re:Newbie question : Outlook 2000, getting info from the InBox folder


Change your code to the following..

Also notice that I changed it to where you only log into the CDO session
once..

================
procedure TForm1.Button1Click(Sender: TObject);
var
  ns: NameSpace;
  tf: MAPIFolder;
  i: Items;
  j: integer;
  ti: MailItem;
  s: string;
  address : String;
begin
  app := CoOutlookApplication.Create; ///JUST DO THIIS ONCE
  try
    ns := app.GetNameSpace('MAPI');
    tf := ns.GetDefaultFolder(olFolderInBox); ///olFolderTasks);

    CDOSession := CreateOleObject('Mapi.Session');  ///  JUST DO THIS ONCE
    CDOSession.Logon('','', FALSE, FALSE);
    try
      Memo1.Clear;
      i := tf.Items;
      for j := 1 to i.Count do
      begin
        try
          if Supports(i.Item(j), MailItem, ti)then
          begin
            Address:=GetAddress(ti);
            s := ti.SenderName+';'+Address+';'+ti.Subject;
            Memo1.Lines.Add(s);
          end;
        Except
        End;
      end;
    finally
      CDOSession.Logoff;
      CDOSession:=unassigned;
    end;
  finally
    App := nil;
  end;
end;

function TForm1.GetAddress(MI: Variant): String;
begin
  Result:='';
  CDOMsg := CDOSession.GetMessage(MI.EntryID, MI.Parent.StoreID);
  Result:=CDOMsg.Sender.Address;
end;
================

Hope this helps.

Paul

--
______________________________________________
Xapware Technologies Inc.
Paul Qualls - Sr. Software Engineer
719.573.4049
http://www.xapware.com

Re:Newbie question : Outlook 2000, getting info from the InBox folder


Quote

> Hope this helps.

> Paul

Thanks Paul, it's alot better, but not entirely solved.
Everything is working fine now...
until I leave the application, then Delphi returns an access
violation. Maybe I forget to cleanup something?

Strange thing is the fact it's working without any problems
on my Win98SE partition, and with these problems on my Win2000
partition. Both the same Delphi, and Office installation.

Thanks,
Gunter.

Re:Newbie question : Outlook 2000, getting info from the InBox folder


I found the reason for this acces violations, GetAddress breaks
his neck when he can't resolve an e-mail address.
This was the case with the Outlook Welcome mail, which is
put in your InBox the first time you run Outlook.
Is there a way to detect this, or avoid it?

Thanks,
Gunter

Re:Newbie question : Outlook 2000, getting info from the InBox folder


send me that message as an attachment and I will look at it.

Paul

Quote
"Legend" <co...@antwerpen.be> wrote in message

news:Xns90DBEDB1F1963ConanAntwerpen@207.105.83.65...
Quote
> I found the reason for this acces violations, GetAddress breaks
> his neck when he can't resolve an e-mail address.
> This was the case with the Outlook Welcome mail, which is
> put in your InBox the first time you run Outlook.
> Is there a way to detect this, or avoid it?

> Thanks,
> Gunter

Re:Newbie question : Outlook 2000, getting info from the InBox folder


Quote
"Legend" <co...@antwerpen.be> wrote in message

news:Xns90DBEDB1F1963ConanAntwerpen@207.105.83.65...

Quote
> I found the reason for this acces violations, GetAddress breaks
> his neck when he can't resolve an e-mail address.
> This was the case with the Outlook Welcome mail, which is
> put in your InBox the first time you run Outlook.
> Is there a way to detect this, or avoid it?

You basically need to check if there is a valid SenderName..  It is silly to
even try to resolve the senders smtp address if in fact there is no sender
name.

Paul

procedure TForm1.Button1Click(Sender: TObject);
var
  ns: NameSpace;
  tf: MAPIFolder;
  i: Items;
  j: integer;
  ti: MailItem;
  s: string;
  address : String;
begin
  app := CoOutlookApplication.Create; //JUST DO THIIS ONCE
  try
    ns := app.GetNameSpace('MAPI');
    tf := ns.GetDefaultFolder(olFolderInBox); //olFolderTasks);

    CDOSession := CreateOleObject('Mapi.Session');  //  JUST DO THIS ONCE
    CDOSession.Logon('','', FALSE, FALSE);
    try
      Memo1.Clear;
      i := tf.Items;
      for j := 1 to i.Count do
      begin
        try
          if Supports(i.Item(j), MailItem, ti)then
          begin
            if trim(ti.SenderName) <> '' then
            begin
              Address:=GetAddress(ti);
              s := ti.SenderName+';'+Address+';'+ti.Subject;
              Memo1.Lines.Add(s);
            end;
          end;
        Except
        End;
      end;
    finally
      CDOSession.Logoff;
      CDOSession:=unassigned;
    end;
  finally
    App := nil;
  end;
end;

function TForm1.GetAddress(MI: Variant): String;
begin
  Result:='';
  try
    CDOMsg := CDOSession.GetMessage(MI.EntryID, MI.Parent.StoreID);
    Result:=CDOMsg.Sender.Address;
  except
    Result := 'not found';
  end;
end;

Re:Newbie question : Outlook 2000, getting info from the InBox folder


Quote
> You basically need to check if there is a valid SenderName..  It is
> silly to even try to resolve the senders smtp address if in fact there
> is no sender name.

> Paul

Paul,

Thanks for your tip, but the problem is that this Welcome Message
from Outlook has a valid Sendername...
Any other ideas?

Gunter.

Re:Newbie question : Outlook 2000, getting info from the InBox folder


If it is the same one that you sent me, then it doesn't have a valid sender
address and that is the problem.

Paul

Quote
"Legend" <co...@antwerpen.be> wrote in message

news:Xns90F4A8F4DBED4ConanAntwerpen@207.105.83.65...
Quote
> > You basically need to check if there is a valid SenderName..  It is
> > silly to even try to resolve the senders smtp address if in fact there
> > is no sender name.

> > Paul

> Paul,

> Thanks for your tip, but the problem is that this Welcome Message
> from Outlook has a valid Sendername...
> Any other ideas?

> Gunter.

Re:Newbie question : Outlook 2000, getting info from the InBox folder


If you check the Message.From and it is blank, then there is no valid sender
name.  I don't see what is so hard about this.

Paul

Quote
"Legend" <co...@antwerpen.be> wrote in message

news:Xns90F4A8F4DBED4ConanAntwerpen@207.105.83.65...
Quote
> > You basically need to check if there is a valid SenderName..  It is
> > silly to even try to resolve the senders smtp address if in fact there
> > is no sender name.

> > Paul

> Paul,

> Thanks for your tip, but the problem is that this Welcome Message
> from Outlook has a valid Sendername...
> Any other ideas?

> Gunter.

Re:Newbie question : Outlook 2000, getting info from the InBox folder


Quote
> If you check the Message.From and it is blank, then there is no valid
> sender name.  I don't see what is so hard about this.

> Paul

Oops, sorry, I gues I was looking too far...
Thanks again for your help.

Gunter

Other Threads