question

EmmanuelDreux-4879 avatar image
0 Votes"
EmmanuelDreux-4879 asked saldana-msft edited

access SharedMailbox using graph api

Hi all,

I want to programmatically access a sharedmailbox without username / password (because Microsoft is going to discontinue it.
For this, I'm using an Azure AD Application and I'm generating an Oauth Token;
The code below works well for accessing any mailbox except shared mailboxes.

If I set the ImpersonatedUserId, I receive an error saying "The SMTP address has no mailbox associated with it."
If I don't set it, I receive an error saying "ExchangeImpersonation SOAP header must be present for this type of oauth token".

Can you give me the trick for accessing the shared Mailbox using Oauth.


_service = new ExchangeService(ExchangeVersion.Exchange2013); // (or later)
_service.TraceFlags = TraceFlags.None;
_service.PreAuthenticate = true;
_service.Timeout = 600000; // 10 minutes
string token = GetTokenForUserAsync().Result;
_service.Credentials = new OAuthCredentials(token);
_service.UseDefaultCredentials = false;
string url = "https://outlook.office365.com/ews/Exchange.asmx";
_service.Url = new Uri(url);
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "sharedmailbox@domain.com");

_fdInbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(_service, new FolderId(WellKnownFolderName.Inbox, "sharedmailbox@domain.com"));


private async System.Threading.Tasks.Task<string> GetTokenForUserAsync()
{
string domainName = _context.MigrationDefinition.TargetConfiguration.DomainName;
string clientId = _context.MigrationDefinition.TargetConfiguration.ClientID;
string clientSecret = _context.MigrationDefinition.TargetConfiguration.ClientSecret;
string microsoftLoginUrl = AzureURL.GetLoginUrl(_context.MigrationDefinition.TargetConfiguration.ServerRegion);
string loginUrl = string.Format("{0}/{1}", microsoftLoginUrl, domainName);
loginUrl = "https://login.microsoftonline.com/" + domainName + "/oauth2/v2.0/token";
string redirectUri = "https://myapp.azurewebsites.net";
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(loginUrl)
.WithRedirectUri(redirectUri)
.Build();

     var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
       
     Microsoft.Identity.Client.AuthenticationResult result = await app.AcquireTokenForClient(ewsScopes).ExecuteAsync();
     return result.AccessToken;
 }


microsoft-graph-sdkmicrosoft-graph-mailmicrosoft-graph-users
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @EmmanuelDreux-4879,·

As according to your description above, your question is more related to Graph API. So I would edit the tags to remove the irrelavant one and add two more graph related tags instead. Hopefully your issue can get resolved soon. Thanks for your understanding!

0 Votes 0 ·

Hi,would you please provide us with an update on the status of your issue?

0 Votes 0 ·

1 Answer

CarlZhao-MSFT avatar image
0 Votes"
CarlZhao-MSFT answered CarlZhao-MSFT edited

The old Outlook API has been deprecated, you can try to use MS graph api to access the shared mailbox. Simply treat it as any other user:

https://graph.microsoft.com/v1.0/users/sharedaddress@microsoft.com/messages

Make sure you have the right permissions set Mail.Read.Shared.


 GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
 var messages = await graphClient.Users["{id or name}"].Messages
 .Request()
 .GetAsync();


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.