1

I have to work with external Exchange server. How can I get the own email address or check address entered by the user (that he introduced exactly own address), using EWS? Email address is not the same as username.

vangog
  • 61
  • 1
  • 8

4 Answers4

4

The best solution at this moment.

You can use ConvertId with a generic address and Exchange will then return the PrimarySMTP for that mailbox eg.

Folder Inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
AlternateId aiAlternateid = new AlternateId(IdFormat.EwsId, Inbox.Id.UniqueId, "mailbox@domain.com");
AlternateIdBase aiResponse = service.ConvertId(aiAlternateid, IdFormat.EwsId);
Console.WriteLine(((AlternateId)aiResponse).Mailbox);
vangog
  • 61
  • 1
  • 8
  • This trick is great for when I am using OAuth, and not using Impersonation. In which case, I could have my script/app running from anywhere/any machine with no other environmental clues to pull from. Thanks very much. – Jeremy Bradshaw Dec 06 '20 at 06:11
  • I put this to use already, now have a function which will likely find its way into all my EWS scripts: function Get-OAuthUserSmtpAddress ($ExSvc) { $ExSvc.ConvertId( [AlternateId]::New( 'EwsId', ([Folder]::Bind($ExSvc, 'Root')).Id.UniqueId, 'OAuthUserSmtpFinder@LargeItems.ps1' ), 'EwsId' ).Mailbox } $Mailbox = Get-OAuthUserSmtpAddress $ExSvc Thanks again. – Jeremy Bradshaw Dec 06 '20 at 06:49
0

You might have some luck with the method ResolveName. Using this method you can search the Global Address List for the user. And by using a simple if else to see if any results were returned. This method does resolve ambiguous names so be sure to check the result carefully

Example:

   NameResolutionCollection coll = service.ResolveName("Johnson", folders, ResolveNameSearchLocation.DirectoryOnly, false);

   foreach (NameResolution nameRes in coll)
   {
      Console.WriteLine("Contact name: " + nameRes.Mailbox.Name);
      Console.WriteLine("Contact e-mail address: " + nameRes.Mailbox.Address);
      Console.WriteLine("Mailbox type: " + nameRes.Mailbox.MailboxType);
   }

If you want to read more about it: https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice.resolvename(v=exchg.80).aspx

Poorya Mohammadi
  • 751
  • 1
  • 8
  • 18
0

Based on iCode4U's Answer, if your service uses default credentials (from the logged user), then this might get what you need:

String address = service.ResolveName(Environment.UserName)(0).Mailbox.Address;

EDIT: If one can't trust the uniqueness of the results brought by the query above, then it is better to use something like this (this would work in my organization, where usernames are also email identifiers, but each one must tweak it to fit his own scenario):

string address = Service.ResolveName(Environment.UserName).Select(
    a => a.Mailbox.Address).FirstOrDefault(
    b => b.StartsWith(Environment.UserName + "@",
        StringComparison.InvariantCultureIgnoreCase));
Community
  • 1
  • 1
VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • No. It does not work. You can get the wrong record. User names may differ by one character at the end. In this case, it will depend on the sorting. – vangog Mar 03 '17 at 14:42
  • Yes, sometimes it can work. But I was looking for a universal solution. When I don't have connection with Active Directory and haven't any email address (not primary). Only username and password. – vangog Mar 03 '17 at 15:19
  • You are right, and that is why I upvoted your answer. – VBobCat Mar 03 '17 at 15:30
0

We use this function loaded in the user PowerShell profile.

    Function CurrentUserPrimarySmtpAddress()
    {
      <#
        .SYSNOPSIS
          Attempt to retrieve the current user's primary SMTP address.

        .DESCRIPTION
          Attempt to retrieve the current user's primary SMTP address.

        .NOTES
          Author: David Barrett
          Date Created: 08NOV2016

        .LINK
          https://gallery.technet.microsoft.com/office/PowerShellEWS-Update-items-48c3dcfc

        .EXAMPLE
          $PrimarySmtpAddress = CurrentUserPrimarySmtpAddress
      #>

      $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
      $result = $searcher.FindOne()

      if ($result -ne $null)
      {
        return $result.Properties["mail"]
      }
      return $null
    }
Chuck
  • 16
  • 3