vangog
vangog

Reputation: 61

How get the current user's own email address or check entered, using Exchange Web Services (EWS)?

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.

Upvotes: 1

Views: 3895

Answers (4)

Chuck
Chuck

Reputation: 26

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
    }

Upvotes: 0

VBobCat
VBobCat

Reputation: 2712

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));

Upvotes: 0

vangog
vangog

Reputation: 61

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, "[email protected]");
AlternateIdBase aiResponse = service.ConvertId(aiAlternateid, IdFormat.EwsId);
Console.WriteLine(((AlternateId)aiResponse).Mailbox);

Upvotes: 4

Poorya Mohammadi
Poorya Mohammadi

Reputation: 751

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

Upvotes: 0

Related Questions