Office 365 IMAP OAuth With C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
with the removal of basic Earth approaching soon we'll need to switch away from it to continue using IMAP programmatically alongside office 365. unfortunately the Microsoft documentation on this is quite lacking the only good information I could found when researching this video was from this guy called code breaks so go check out his video the first step will be first to register an ash application to give us the proper permissions so open up the Microsoft 365 admin Center and from here we'll head over to Azure active directory and click on app registrations now I'm just going to call my application IMAP or2 and I'm going to click register here we'll make a note of these three variables as we'll need them later for now though we'll add API permissions to the application so I'm just going to remove the default use the read and we're going to click on ADD permissions then API is my organization uses search for Office 365 and choose to exchange online with application permissions and from here we can get the imap.access as app there's also an option for POP3 in case you need that for a legacy system then we'll Grant admin consent for the application and this will require you to be locked in with an admin account so in case your account isn't one this would be the time where you would need to ask someone else to Grant taxes now I know we'll need to create a client secret as well but when doing it here we can only choose a 24 month lifetime for it whereas if we use Powershell we can create secrets that last 99 years so with our graph application made will for this next part need to open up Powershell with administrator access and we'll install two modules if you don't already have them the first module is Ash 80 and the second module is exchange online management then we'll use connect 80 log in with an ADM account for your tenant and then we'll use connect exchange online and do the same now we're connected throughout Zenon to Azure active directory and exchange online with Powershell giving us access to a whole bunch of commands first thing we'll do after connecting is to create a variable containing the Azure ad service principle that belongs to the Azure application we created in the last step we'll do this by calling git Azure ad service principle and here's a search string we'll input the name of the application which in my case was IMAP oauth 2. then we'll call new service principle using the app ID and object ID from the variable we just created and then assign it a display name what this does is create a service principle and exchange online that we can assign permissions to and it will carry over to our Azure application because of this we can then assign the service principle full access to our mailbox and we can gain access to it in our program later we'll do this by calling the add mailbox permission commandlet here we'll pass in the mailbox we want access to as the identity our object ID from our app as the user and full access as the access rights the service principle can have access to multiple mailboxes in case you need it just use the same command and change the identity as needed now as mentioned the last thing we'll need to do in Powershell is create a secret for our Azure application which will use later to connect so let's create a variable called start date and set it to get date then we'll create a variable called end date and assign the value of start date plus 99 years since we're still connected to Aztec directory we can use the commanded new Azure ad application password credential which takes the object ID of our Azure application a custom key identifier which is just a display name a start date and an end date now the value you get here as an output is the secret so make sure to save it as you won't be able to view it again now we're done with the Powershell part and we can start coding but before we fully dive into C sharp I want to show you how you can get the access token you need using any language in C sharp people get the token in quite a few different ways but we could just post to a URL as any other API and get the token from there so I just wanted to show you here you just want to make sure that you pass in the parameters in the body as form data or form URL encoded and the grant type has to be client credentials and the scope the Outlook URL you see here this Outlook URL is required as the scope if you're using an application so now you know how to get the access token and you can do it your preferred way personally this is how I add to it I'll make sure I can access the variable we need to post here I've just stalled them in a separate file for security purposes you probably want to do this differently in the real application and we'll create a method for getting the access token which would be a static async task returning an axis token model which is just the model I've created with the full values I expect to receive from the call then we'll Define the URL to post to and create a dictionary without values to post that will create an HTTP client to use in the using statement and then call Post async on it and pass in the data then we'll return the content populated into an access token model for the IMAP part of our program I'll be using a library called make it as it's free and supports ol2 we'll start off by defining the mail we want to connect to the host which will always be outlook.office365.com and the port which will be 993. this is all defined as by the Microsoft documentation and for my demonstration here I'll create a method for connecting I'll call it an IMAP connect async and it will take an IMAP client and access token and default the access level the folder access level is what sells it if we can read write or both I'll create a new sasl mechanism or two variable here and pass in the mail we want to connect to and then the access tokens that will be used instead of the password we'll then call connect async on the IMAP client and pass in the host port and what security to use the default for Microsoft here will be TLS 1.2 next up is authenticating using the old 2 variable now in my case I'll just open the inboard here you could of course open a different folder just be aware that you have to open a folder before you could read any data from the mailbox then we can create a method here that returns all messages from the inbox it would be of type task and return the list of my messages which is the format we get emails in when using mail kit so we'll create a new IML client and use it in using statement then call our IMAP connect async method after this point we connect it to the mailbox and can access data using the IMAP client so here we'll get all unique IDs of the males in my inbox by calling search async and passing in the search query for all then we'll Loop through them and call get message async pass in the unique ID of the mail and add it to a messages list then we'll disconnect and return the list using almost the same code we can create a method to give us the first Unwritten email message in my inbox like last time we'll create an IMO client we'll connect to it we can then access client.inbox.first unread get that message asynchronously and return it to put it all together all we need to do is call the get access token method on our API Handler then call it get oil messages on a mail handler and then we can print the subject of each methods we can do the same thing with the first unread message let's call the get method for that and print the subject we can then run our program and see what it prints now if we show you here in Outlook online you can see that it printed the correct things I've just used the shared mailbox in this case but you can use an ordinary mailbox as well and that's it that's how simple it is to use ol2 with Microsoft 365 and c-sharp in case this was useful feel free to subscribe like the video and if you have any comments I'd love to hear them
Info
Channel: Blue Hippo
Views: 20,634
Rating: undefined out of 5
Keywords: c#, office 365 oauth, modern authentication, mailkit
Id: eDTO9W81P-Y
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Sun Oct 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.