Retrieve Outlook logged-in user SMTP address after

2020-02-02 02:46发布

问题:

Exchange Web Services has a ResolveNames() function that I can use to retrieve (among other things) the primary SMTP address for the Active Directory user that logged on to Exchange Server through EWS.

I am now programming through OLE against Outlook and would like the same functionality.

I have been browsing through the Outlook object model but can't find an appropriate object or method.

Does anyone know of an object/method that I can use to get the primary SMTP address?

Below is the current Delphi code that I use to connect to Outlook.
For the default user logging in (AUserSMTP='') it returns the OutlookApp COM Object (through GetActiveOleObject or CreateOleObject), a NameSpace (through GetNameSpace) and a Folder (through GetDefaultFolder) object, but I could not find where to go from there.
I thought lNameSpace.CurrentUser (a Recipient object) might lead somewhere, but its Address property only returns a string like '/o=TimeTell/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=developer' without email address...

Any suggestions about the route to take?

function TDataModuleSyncOutlook.ConnectToOutlook(AUserSMTP: String = ''): Boolean;
var
   lNameSpace, lRecipient: OleVariant;
begin
   Result      := false;
   FWasCreated := False;  
   try
      FOutlookApp := GetActiveOleObject(scxOutlookApp);
      Result := True;
   except
      try
         FOutlookApp := CreateOleObject(scxOutlookApp);
         FWasCreated := True;
         Result := True;
      except
         on E:Exception do ...
      end;
   end;
   if Result then      
   begin
      lNameSpace := FOutlookApp.GetNameSpace(scxNameSpace);
      if AUserSMTP <> '' then   // This part not applicable to the question   
      begin   // Open shared calendar als er een expliciete gebruiker is opgegeven...
         lRecipient := lNameSpace.CreateRecipient(AUserSMTP);
         try
            FCalendarFolder := lNameSpace.GetSharedDefaultFolder(lRecipient, olFolderCalendar);
         except
            on E:Exception do ...
         end;
      end
      else   // ... anders de default calendar folder openen
         FCalendarFolder := lNameSpace.GetDefaultFolder(olFolderCalendar);
   end;
   FOleInitialized := Result;
   if Result then TSyncLogger.LogAlways('Connected to Outlook') else TSyncLogger.LogAlways('Connection to Outlook failed');
end;

回答1:

Why not use Application.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress (you would of course need to check for nulls)?

As for the account order, you can either use Extended MAPI and IOlkAccountManager.GetOrder (you can play with that object in OutlookSpy if you click IOlkAccountManager button) or you can use Redemption and its RDOSession.Accounts.GetOrder method (see http://www.dimastr.com/redemption/RDOAccounts.htm). The first account in the returned collection will be the default one.



回答2:

I found it. I have to go through the Accounts object in the namespace:

for i := 1 to lNameSpace.Accounts.Count do
   if lNameSpace.Accounts.Item[i].AccountType = olExchange then
   begin
      lAccount := lNameSpace.Accounts.Item[i];
      Break;
   end;
if VarIsClear(lAccount) then
begin
   DisConnectFromOutlook;
   Exit;
end;
lLoginSMTP := lAccount.SmtpAddress;

The only thing I would still like is to determine the default account.