Board index » delphi » Getting return email address

Getting return email address

Hi

I have been working with outlook 2000 com objects and can succesfully
messages from my deleted folder and inbox.  The only problem is that there
doesn't seem to be a mailitem property that contains the email address.
There is a senders name which is OK if it contains an email address.
The problem it causes is when I send emails and doesn;t check in the address
book properley and misses out any that just have names.

Here's what I've been doing :

var
  OutlookApp,
  Mapi,
  MailItems, mi,
  CurrentContact:       Variant;
  i : integer;
  cTo : string;
begin
  OutlookApp := CreateOleObject('Outlook.Application');
  Mapi := OutlookApp.GetNameSpace('MAPI');
  MailItems := Mapi.Folders('Personal Folders').Folders('Inbox').Items;

  with ContactTable do
  begin
    EmptyTable;
    Open;

  for I := 1 to MailItems.Count do

    begin
      CurrentContact := MailItems.Item(i);
      Insert;
      FieldByName('EntryId').AsString := CurrentContact.EntryId;
      FieldByName('EmailAddress').AsString := CurrentContact.SenderName;
(this is the nearest I can find)
      Post;
    end;
  end;
  OutlookApp := Unassigned;
end;

Then when I send mail :

const
  olMailItem = 0;
var
  OutlookApp,
  Mapi,
  mi,myRecipient,
  CurrentContact:       Variant;
  lOK : boolean;

begin
  OutlookApp := CreateOleObject('Outlook.Application');

  ContactTable.Open;
  ContactTable.First;
  while not ContactTable.eof do
    begin
    if ContactTable.FieldbyName('EmailAddress').AsString <> '' then
      begin
      MI := OutlookApp.CreateItem(olMailItem);
      mi.ReadReceiptRequested := false;
      myRecipient :=
mi.Recipients.Add(ContactTable.FieldbyName('EmailAddress').AsString);
      mi.Subject := 'This is the subject';
      mi.Body := 'This is the message itself';
      if myRecipient.Resolve = true Then
      mi.Send;

      end;
    ContactTable.Next;
  end;

 

Re:Getting return email address


<<Andy Dyble:
FieldByName('EmailAddress').AsString :=
CurrentContact.SenderName;

Quote

You can get the email address of the sender by createing a
temporary reply to the message:

var
  MI, Reply: MailItem;
begin
  MI := Inbox.Items.Item(Inbox.Items.Count) as MailItem;
  Reply := MI.Reply;
  Caption := Reply.Recipients.Item(1).Address;

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Getting return email address


Thanks, I'm nearly there.

With your bit of code and some messing around I can get the Inbox email
addresses perfectly.  I then copied the code onto another button to get the
deleted items addresses.  The code below is the same bt for the name of the
folder.
Te only problem here is that there are 1131 deleted items and if I track the
i variable as below it stops at 546 thus halting the program completely.

There are quiet a few that don't have a return address for some reason but
I skip them.

FYI - there is an Outlook App server object from the D5 tab on the form.

Any more thoughts would be grateful.
Andy

Code:
------------
procedure TLoadTableForm.Button1Click(Sender: TObject);
var
  i, j, k: Integer;
    EmailAddress: string;
  NmSpace: NameSpace;
  Folder: MAPIFolder;
  Mapi,  Contacts,CI : OleVariant;
  Personal, MyFolder, PostVak: OleVariant;
  MailItem2,MyItem: OleVariant;

begin

    AdsContacts.Exclusive := true;
    AdsContacts.oPEN;
    AdsContacts.AdsZapTable; // file://uses advantage database engine

    MyFolder := 'Deleted Items';
    OutlookApplication1.Connect;
    MAPI := OutlookApplication1.GetNamespace('MAPI');
    Personal := Mapi.Folders(Mapi.Folders(1).Name ) ;
    PostVak := Personal.Folders( MyFolder );

    showmessage(IntToStr(PostVak.Items.Count));  // shows 1131
    for i := 1 to PostVak.Items.Count do
      begin
      try
        Application.ProcessMessages;
        Edit1.Text := IntToStr(i);  // stops at 546

        MyItem := PostVak.Items(i);
        MailItem2 := MyItem.Reply;

        EmailAddress := MailItem2.Recipients.Item(1).Address;
        if EmailAddress <> '' then
          begin
            AdsContacts.Insert;
            AdsContacts.FieldByName('Email').AsString := EmailAddress;
            AdsContacts.Post;
          end;
      except
      end;
    end;
    showmessage('Complete');

end;

"Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
message news:VA.000008ae.002ce0a9@cableinet.co.not-this-bit.uk...

Quote
> <<Andy Dyble:
> FieldByName('EmailAddress').AsString :=
> CurrentContact.SenderName;

> You can get the email address of the sender by createing a
> temporary reply to the message:

> var
>   MI, Reply: MailItem;
> begin
>   MI := Inbox.Items.Item(Inbox.Items.Count) as MailItem;
>   Reply := MI.Reply;
>   Caption := Reply.Recipients.Item(1).Address;

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   Use Borland servers; TeamB don't see posts via ISPs
>   http://www.borland.com/newsgroups/genl_faqs.html

Re:Getting return email address


Update :
i've pretty much got it working now,
I put some code in the except bit seems to have cured it.

One more problem has arisen though.
After inserting about 150 records the dial up network connect box come up
and you can't get rid of it or run Outlook 2000 until you connect.

Thanks again

Andy

Quote
"Andy Dyble" <sa...@mid-soft.co.uk> wrote in message

news:94k9pf$fcr7@bornews.inprise.com...
Quote
> Thanks, I'm nearly there.

> With your bit of code and some messing around I can get the Inbox email
> addresses perfectly.  I then copied the code onto another button to get
the
> deleted items addresses.  The code below is the same bt for the name of
the
> folder.
> Te only problem here is that there are 1131 deleted items and if I track
the
> i variable as below it stops at 546 thus halting the program completely.

> There are quiet a few that don't have a return address for some reason but
> I skip them.

> FYI - there is an Outlook App server object from the D5 tab on the form.

> Any more thoughts would be grateful.
> Andy

> Code:
> ------------
> procedure TLoadTableForm.Button1Click(Sender: TObject);
> var
>   i, j, k: Integer;
>     EmailAddress: string;
>   NmSpace: NameSpace;
>   Folder: MAPIFolder;
>   Mapi,  Contacts,CI : OleVariant;
>   Personal, MyFolder, PostVak: OleVariant;
>   MailItem2,MyItem: OleVariant;

> begin

>     AdsContacts.Exclusive := true;
>     AdsContacts.oPEN;
>     AdsContacts.AdsZapTable; // file://uses advantage database engine

>     MyFolder := 'Deleted Items';
>     OutlookApplication1.Connect;
>     MAPI := OutlookApplication1.GetNamespace('MAPI');
>     Personal := Mapi.Folders(Mapi.Folders(1).Name ) ;
>     PostVak := Personal.Folders( MyFolder );

>     showmessage(IntToStr(PostVak.Items.Count));  // shows 1131
>     for i := 1 to PostVak.Items.Count do
>       begin
>       try
>         Application.ProcessMessages;
>         Edit1.Text := IntToStr(i);  // stops at 546

>         MyItem := PostVak.Items(i);
>         MailItem2 := MyItem.Reply;

>         EmailAddress := MailItem2.Recipients.Item(1).Address;
>         if EmailAddress <> '' then
>           begin
>             AdsContacts.Insert;
>             AdsContacts.FieldByName('Email').AsString := EmailAddress;
>             AdsContacts.Post;
>           end;
>       except
>       end;
>     end;
>     showmessage('Complete');

> end;

> "Deborah Pate (TeamB)" <d.p...@cableinet.co.not-this-bit.uk> wrote in
> message news:VA.000008ae.002ce0a9@cableinet.co.not-this-bit.uk...
> > <<Andy Dyble:
> > FieldByName('EmailAddress').AsString :=
> > CurrentContact.SenderName;

> > You can get the email address of the sender by createing a
> > temporary reply to the message:

> > var
> >   MI, Reply: MailItem;
> > begin
> >   MI := Inbox.Items.Item(Inbox.Items.Count) as MailItem;
> >   Reply := MI.Reply;
> >   Caption := Reply.Recipients.Item(1).Address;

> > --
> > Deborah Pate (TeamB) http://delphi-jedi.org

> >   Use Borland servers; TeamB don't see posts via ISPs
> >   http://www.borland.com/newsgroups/genl_faqs.html

Other Threads