How To Send Email Using Microsoft Graph API with Dot NET Framework or C# #developer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello uh what's going on guys welcome to the channel uh it's been a while since uh I created a new tutorial uh but I'm back again so uh today we're going to focus uh on the Net Framework or c-sharp so we're going to look at how we can access Microsoft 365 resources using Microsoft graph API with um using.net uh framework okay so we're going to focus on sending an email using.net framework with Microsoft graph API so the same concept that you're going to run to learn today uh you're going to apply it to any other custom projects that you might have within Microsoft's graph API uh using.net framework for example if you want to be able to access to get the access token you want to be able to retrieve all users you want to be able to read mailboxes as using NET Framework with Microsoft graph API you're going to use a these high-level um um understanding of what you're going to do today and apply to those custom projects okay so um the first thing that you need to do um uh to to be able to run a.net framework within your system is to install the.net framework um SDK so for me I'm using.net SDK version 7 64 bitfall because my system is 64. so for you to download dot SDK I just opened a new tab here and then I'm going to type in uh dot net core this is it's there but net core SDK so it will you'll come to this uh website Microsoft website you can see they'd start with DOT net dot microsoft.com and then from here you can download the version that meets your requirement so we have.net 6.0 for my case I just went with 7.0 64 bit for Windows since I'm using Windows okay so the text editor that I'm going to use it's the vs code Visual Studio code so you need to also download visual studio code and install it in your system okay so once you do that we can proceed I'm not going to show you how we can go about downloading Visual Studio codes and something that you can do from your site and also downloading the.net it's something that you can do is just like another application that you download and install it in your system okay so once you get the vs code text editor installed in your system and also you have downloaded and installed the SDK within your system you can just open the whatever file uh directory that you prefer and create a folder So within the drive that you specify you have more than one drive in your machine uh you can just go there and create a new folder for example I went to desktop and created a new folder named.net tools so once you create the folder the next thing that you just need to do is you can open it like that and then from here you can just type CMT to open command prompt within that current directory where we have that folder and once you open there you can just type in dot net new console just like that and then what it will do it will create it will automatically create a project for us a.net framework uh project for us or c-sharp and then one is created or you just need to do to open the vs code within your system it's just type in code Dot and then enter and then you can see there we have program.cs and also we have uh the dot net CS project so these are this is created automatically for us by that command the.net new console and then we have also a food that they are uh Dean we have for DJ so this will be created automatically for you by that command so what we're more interested uh with is the program.cs because here is where we're going to write our code okay and um something else um as a prerequisite for this project is uh since we're going to interact with Microsoft graph API you also need to register an app registration in M365 or ajd so in my Azure ID so yeah this is the tenant that I'm going to use so here I have registered an application app underscore new and then the only thing that you need from the app registration is the application client ID this one we need also the tenant ID and something else that you also need to provision is to secret for that you're going to use and something else also is you need to also assign the permission an API permission so since we're going to send an email uh in this tutorial you need to assign your app registration mail dot send permission so as you can see this is what we need mail.send so if I can go to Microsoft's website here a documentation that is you can see for us to be users like we have colonies and mail we need to assign our application within application needles and mail dot send so I also have another video within my channel that demonstrates how you can create or how to create an app registration and assign any permission that you need for you to be able to access the respective resource within Azure ID or m365. so once you create your app registration you're going to grab the application client ID the secret and also the tenant ID so if I can just open the vs code where we've created our project so we have program.cs we can test this one to see if it works actually so to test it you just need to launch um terminal you can just click terminal the terminal tab in vs code and choose new terminal there and here I'm going to just type dot net space run and then it's going to execute and then there we go we have Hello uh wallet so whatever we have here this is our starting point so we can see that uh project is working so now we're going to jump in into now the uh our main focus which is now to interact with Microsoft graph graph API so the first thing that we're going to do is we need to import um uh module so I'm from python site so I'm not I'm not that much uh into that NET Framework but I understand it since I usually interact with it when working with Microsoft graph API projects that I have but I'm not a com uh dot NET Framework developer so I might like confused with my warnings Okay so we're going to import so you can just remove everything there and um to begin it I'm just going to say using so he is whatever we're going to use within our project so like in Python we usually import like if you want to use the OS module you just say import or so in.net framework they usually say you start with saying using and then the name of whatever package or um module that you're going to interact with in your code so using uh system dot collections sorry I'm not going to use the collection so I'm going to use dot net and then we have dot http dot where does I can just start semicolon there so next is using um system colon there and then we have using system Dot that's Dot Json so these are built-in modules within.net framework so we're going to for for now we're going to import those ones so this is what we're going to interact with within your within our code so NX is to create the best of our project which is now the program class so in.net they fly more in class it's like Java so the first thing is to create a class which is the base of our program and it's going to be class program just like that okay next I'll be going to initialize it's like initialize now our main class for for for our project so we're going to call it main so static testing task Main and then we have string which is uh an empty array I believe and then press UPS ads [Music] no lights throwing that error always we need space here you have static acing task Main and some are called on that okay so um let me just.net slash here do you see what is going to happen you see guitar error is going to go away so we have an error there identify okay so I added like um that's comma there so it's the one which is called bringing model server so this one now you can ignore it just a simple one it's not an error so I just wanted that to go away so we are good so to proceed not um now here we need the first thing that we're going to do is to Define uh up up registration uh details and then now we we create we we write a code for exporting the access token Okay so since I have this in a notepad in your case you can just copy it in a notepad provision you create a string client ID so that will host the application client ID and then another variable client secret to store the secret and then we have the tenant ID which is also going to store the internet ID so I'm just going to copy that I add that you know txt file so I'm just going to paste it there just like that let me see if this one okay good to go so just like that okay so I can just add a comment here just so we can keep our code clean so uh registration so oh it's going to create Azure app registration info just like that so next we're going to create an HTTP client to make requests uh for for now to make requests to the graph API to get the access token so we need an HTTP client object so if I can just add a comment there and say create an http plan so here we're going to say using into brackets HTTP the thing that we had imported there at the top and equal initialize it new and then HTTP client just like that I can put this one in the next line like that just not skip the line there to keep our code clean so here we're going to create a new variable bar and then call it requests content bar Quest content it's new going to call inform URL encoded content that should be in a bracket and then new the scale brackets and then now we have our code there so that's where our code is going to lie so again put just that one in next line to look like that this one also I need to put it in the next line so okay we are getting a couple of Errors okay I don't know where that is coming from everything looks good foreign so HTTP client HTTP client client new HTTP client I don't know where this area is coming from so new can I read there but let me just continue writing to see [Music] [Music] okay so let's proceed to a new uh key value pair key value pair just like that it's going to be a string string it's going to host run so this is how you define the graph API details here so the grant type the first thing that you're going to Define so this this one is usually like static doesn't change for whichever language that you're using to access Microsoft graph API so this one is I believe it's defined by Microsoft themselves so client credentials so gun type is client credentials so we can see there it's gone so that's why we need just to proceed so you can move next so they next thing if I can just copy this one so that we don't just keep on typing the same thing over and over so this new give value pair as well and it's going to be a string string the type is going to be client ID so here it's our client ID just like that and then here we have our client ID there at the top so the variable that is hosting client ID we're going just to pass it there and you can see that Kayla that yellow thing it's gone now so next uh it's going to be client Secret client regret here I'm just going to remove the client credential and passing the client secret variable there next is going to be um the scope so the key value pair ground type so this one is going to be a scope what will be scoping when accessing Microsoft graph API so here we're just going to pass in the URL so it is usually https colon graph dot Microsoft Microsoft .com and it's slash dot default just like that so that is the HTTP client and we have defined it using the um the Azure app registration ready to make the request to the graph API so next um we're going to just join in there a second foreign info so we're supposed to uh to type in the end point for getting the access token so it is also a stream so I'm talking endpoint I'm going to call it token endpoint and then is a URL so I'm going to start with a dollar um character there so the dollar character just the way in Python we can use the F string to be able to add variables within a string or concatenate uh string with the variables in Python so in dotnet they usually use the uh the the dollar uh the dollar character so um so I'm going to define the URL so it's https so that is the end point to get the access token so again dot Microsoft so this one is static you just need to type it the way it is and the you it's just automatically replace the tenant ID within the URL so Microsoft uh online dot com and then we're going to pass here now our tenant ID variable there attendant ID like that next is or out or out to slash version we're going to use 2.0 [Music] and then slash talk can then now semicolon there yeah so that is the end point that we're going to call to get the access uh token so here we have our HTTP client to get the access token so let's proceed next which is now to now get the access token now retrieving the access token so down there you can just proceed and bladder comment there which is token okay the first thing that we're going to Define is https sorry it's supposed to be http HTTP response message HTTP response message sorry it's cheap press pause message reads response I'm going to await plan so now the HTTP client that we defined there now we're going to use it so our client go to posts the most request then I'm going to pass in token endpoint and the request content which is here the one that we defined there so request content like that got a semicolon there next string a variable to host now the response so response response content I'm just going to use the same variable response Dot content dots read async read read as string sorry read as string I think foreign so request content sorry it's not not to request content but response response content just like that next is variable to pause now the token response so I'm going just to call it token response so token response links calling Json some serializer Dot deserialize Json elements going to pass in our response content there don't forget the semicolon await Json serializer dot serialize Json element then you have a column there I think I made a mistake it's not supposed to be a wait for the response since we're going we are just taking the content and digitalizing it so here I'm going to create another variable from stream to store now the access token going to I can sorry token response whatever now we have got from the endpoint or from the requests so token response dot get and property so we're only interested to get the access token from that this your lies Json data so success dot can and we're only going to get the string that string like that is a function just like that so string access token so the access token is a variable to host the actual access token and then we have the token response here the one that we have already received and deserialize it and then we're going to grab only the access token property okay so next what we can do is just to test to see if it works we can do a console uh supposed to be console.log um or that right line sorry console.writes and then from there we can pass in the access token just like that you can put in a semicolon there and we can run this code to see if we can get a graph API access token so you do that it's just from the terminal.net space brand and then you wait and they are boom although we have a warning uh in the terminal but it's no big deal but we have access token to make any request using the graph API so there we have the access token and that is how simple that you can get access token to access Microsoft graph API uh with NET Framework or c-sharp okay so I'm just going to put a comment here so next now we're going to do the uh actual request to graph API to be able to send an email okay so I'm going to send an email from an account in M365 which has a mailbox to an external email address to a Gmail so I'm going to send to my personal Gmail account thank you okay so once we have the access token Now we move next which is now to Define uh the API endpoint to to send the email we Define the user account that we're going to use to send the email from okay so the first thing is to create the um the variable to host the account that you're going to send an email from so I'm going to call it string mail user okay so string male user I'm going to use my admin account so Bernard at the corner comes dot on Microsoft .com like that so if you can just go to my tenant I have that user sorry I can just go to n365 so this is the account that I'm going to send from so it has a license and there's a mailbox if you can just go to mail tabs is to show you there so we have the mailbox settings there you can also open Outlook or labs Outlook so there we go so I have a mailbox with that account so we can proceed next so that is the account that I'm going to use to send email from so don't forget to answer my call on there so moving next is now to define the URL the endpoint you call it an endpoint that will now do the magic of sending the email so it's going to be a stream so send mail endpoint just like I said use in.net we usually use um uh a dollar a dollar character to be able to code concatenate strings so https I'm going to call in graph dot Microsoft microsoft.com [Music] the version of the API so I'm going to use V that is version 1.0 slash users and then there we have slash I can pass in the user principle name so this is the user principle name of the email address for the account so it's going to be mail user just passing that variable just like that next is to passing send me and semicolon so this one is already defined the URL for each actually for any any um request or any endpoint that you want to uh to make a request in Microsoft graph API within Microsoft documentation we already have this so be it you want to be able to retrieve users So within Microsoft documentation usually get those URLs it's only that when you have like a complex request that you want to make in Microsoft graph API that you will need to uh construct your own custom um uh URL or endpoint to match your requirement for example if you want to do your own filtering stuff like that that's why now you can be able to construct your own custom uh endpoint on URL so if you can come here and see is from Microsoft documentation you can see to make a post request first it's a post request when you're trying to send an email using graphic Pi so the content type there we have application slash Json and then you can see the end point the way it looks like so for me this is to stand from the current account that you're using so instead of just going with me for me I just chose to go faster calling or users and then now access that specific user which is now my account and then now the send mail it will work the same so any resource that you are making you want to access using graph API Microsoft usually defined for you the the end point for example this is for drive so if you accessing SharePoint or OneDrive the end point will be defined for you there and then you can make your own personal customization so as you can see there we have made slash Drive we have for users specific user and then you access that drive for that user okay so proceeding next Once We construct the endpoint to send the email next is to uh Define the message so the message block or the sniper you're going to create the snipper that we're going to use to now compose the email so I'm going to call it uh VAR message and then it's going to be a dictionary string object just like that so this one we might need to end it to the semicolon so that we throw those errors away so once you define the message block next is um foreign just like that so that is starting the way it is then we begin with um and start now we Define our message it's going to be new dictionary so our string string so this one is not a string string this is string object just like that and then there we can Define the the subject so the first thing within the block of the message that's the way we usually do when you we are sending an email we need to define the subject for message so subject so this is going to be tests email from an office so I'm going to test email using graph yeah so that's going to be the subject of our email next we're going to define the body foreign so the body of our email is going to be new summary to be a string that this one should be on the next line like that right from here now we can Define the content type of our message or the body so it's going to be content first thing is the content type which is a text next we need to define the um the content itself or the message so the body now so it's content so I'm going to type in something like hello this is a successful message sent by Microsoft graph api.net rainbow so let me just wrap this up so that it doesn't go overboard just like that so that is going to be the body of our message of or our email just like that simple stuff that is the body so we can move next uh which is to now Define uh the we need to so separate this one with a comma so next is going to be the the recipient so we need to define the recipient of our message so it's going to be another calibration Island and then from here we can Define CPM CPN s I think that is typed correctly object [Music] and from there we need to define the new summary because this one will just to be a dictionary another dictionary of string object foreign so this one you can just follow up the way it is I'll I'll share the code uh within my GitHub account so that you can just uh take a look at it and also if you need to do your own personal customization uh you can do it so there will be a link at the video description uh that you can just go and access the GitHub my GitHub account and you can uh you can Fork is it for the project then your GitHub account and then you can access it or you just copy it and use it or customize it to match your own custom project requirement okay so once you define the dictionaries now is to define the actual email address that we want to send the email to [Music] it's also going to be wrapped in another Cali brace wow so this is the email address for our recipient address just like that then you can Define new dictionary of type string object just like that so here is where another you know the actual address course so address in my personal Gmail account gmail.com just like that simple as that so they can just try to minimize here we see what we have problems and there we go it's not even that huge but you can see um so something else that you might need to also understand is the reason as to why you can see we have Progressive dictionaries uh objects within our code is because that's how the resource is defined within Microsoft graph API so in Microsoft graph API we have to be able to get the address that you need to send the email to you have to go through this route so there is two recipients and then two recipient is also opens to an email address and then we have address and from addresses then where we're going to define the actual email address for our recipient so that's how the resource is represented in Microsoft graph API okay so once we have the recipient defined I can just try to assume foreign [Music] the message we needed to remain in our send items folder so we need to store that email that we have sent you know send uh send items folder within uh account so here we need to store that email here as well so that we have a record or not we have sent so to do that uh we need to where we have a variable message let's see just below here I believe supposed to be below this one where we have where we are saying where the message you're defining it so just below this calibrates here so here is where we need to Define that then to just retain that message that you have sent to save it in our in our send items uh folder the way we do that it's just type in save to send save to send items okay just like that and then you can put it to true yeah so save to send item set it to true just below that calibration we are defining the actual message or uh email uh detail okay so once we Define uh the message the next thing will be to now uh move next which is to now create a logic to send the email okay so we already have done so we have done the access token we have uh done also the endpoint to uh to send the email and also the account that you're going to use to send the email from and also they are email message so the next is now to create a code or to just um to create a code that now will do the magic for sending so what we need to do is do the first thing is to create an HTTP content object that which is will be in a Json format to send the email so the first thing you need to define the sub variable we call it Json message and send a message and we're going to pass in Json serialize possibly Json serializer like that and then Dot serialize passing the message so this message is here the variable that now the whole object of our message so we're going to pass it in there don't forget to add and see my colon next is to now create the word for Content so this one really uh this one will Define the type of uh the contents that we are making through Microsoft graph API like the content type of our message so the content into the new stream some content like that passing out Json it's a message there the one that we have just defined the UTF this is now encoding so UTF ETF supposed to be UT F only just look at it supposed to be encoding encoding dots UTF eight and supposed to be an application application slash Json or the semicolon so that one looks okay next is uh now we make the post request now to the graph by API itself so the plus request itself now will be bad create a request [Music] call it new supposed to be http um HTTP request message http message that one so automatically the way you see this code like doing the outer population so when you're starting your your project your.net framework project uh it will the vs code automatically it will ask you to download the.net uh extensions so there are some couple of extensions that automatically it will ask you to install it for so you need to install the c-sharp and you'll be able to see this HTTP request message come automatically for you so when you start typing it will just appear there if you understand what the function of the class is doing so HTTP request message accepts HTTP method which in our case is post now access now we need to send email so here we're going to pass in the variable that is hosting a endpoint or the end point that is sending the emails or this one so we can just copy the way it is there so that is that these are request so we do a post request through that URL or endpoint which I need to be in a semi colon as well next uh you need to now do the information you need to send the request with permission so it's going to be request headers passing in the access token now authorization [Music] authentication add a value so it's supposed to be authentication header value this one accepts data and then now the access token like that for semicolon in that error next is now to get uh now the content so requests so we need this we need to also send with the type of our content so it's going to request the content and equal plus two and then there it's like that so already if I can just add a comment there just to know what you're doing so this is the post same Quest and the email that's how you do it so now we can send the email itself we're going to call http a response message again [Music] read response message I'm going to define a new variable which is now send me response send me response so we need to avoid this one and then now our HTTP client there we call it dot send async passing request semicolon [Music] and I think I think everything looks okay there nowhere the area is coming okay so next is string and name response send mail response content say await also then mail sentiment response our body at the top so foreign from this one HTTP response sent mail response send mail response it's not supposed to throw an error send mail response dot contents that readers sing so I think here we have an issue let me see okay so what I'll do I'll just ignore this one and then move next to see what will happen so let's verify so this one will just verify If the message goes successfully or not so if send send response it is oops sense that is called also let's delete that does console right [Music] now [Music] and successfully so if the email go successfully without a problem we'll get that printed so his success that is called so client is on just think there's something I'm missing out http oh sorry so it's not supposed to be request so we have a request here so these ones since we are reading whatever we have we have done already the post so we need to get a response so it's supposed to be response so that is why I was a little bit yeah it's supposed to be response message like that so we have already made the request so the next is to get the response of our request and read it so um to be able to read and get the code so if it returns uh successfully the email goes successfully you will get email sent successfully printed in our console and if the email is not sent successfully we need to do announce a block there and add in a console also the straight line is supposed to say failed exam email yeah and then you complete that with SMI color and that is done guys so just have something here which is a warning but it's not a big deal so we can just call it say wrap call chain wrapping a line called chain so we can just ignore this one since it's a warning it's not an error and there we can see uh the code is there so that's how you you do the plus request to to the graph API to be able to send the email the Sam Samsung concept that you see or the principle that you see when you're doing the sending the email which is a post request applies to any other custom thing that you might need to achieve using graph API that requires post requests for example if you want to edit if you want to upload a file in SharePoint if you want to upload a file in one file you will use post and that's how you can do the request so next guys is just to run our code and save you that's what we have already done or we get an error so supposed to be dot net and then run so give it some time and boom guys you can see we have email send uh successfully so next is to verify you have received the emails or the is supposed to receive a lot this is a successful so the time is 15 16 here so I can just go to my Gmail account there's open a new tab and my personal Gmail account password is as opposed to my Gmail account just close that one so there we go we guys we have it so the email there that's already 315 so we send it to 315 something just one minute ago zero minutes ago so hello this is a successful message sent by Microsoft graph API within.net framework and the subject is test email using graph API just the way we Define it in our code and uh also guys we can do it so if you want to send another email with a different subject message or you want to I can just a lot this is a successful message you can say message you can just edit subject two and I can send the email again.net run you can see their emails sent successfully will go to the account you refresh another email there so you can see zero minutes so hello this is from message message to subject two so that's all guys uh this thing is very simple how you can interact with Microsoft graph API using.net framework or c-sharp so if you have any questions uh or you might need to maybe point out something that was not clear Within These uh tutorial you can just put in the comments below and I will answer you guys so um there is more that is coming within Microsoft graph API and that is uh we'll create a video just to show you how you can interact with OneDrive using Microsoft graph API and that is now using python using also Powershell so we will be just concentrating on those uh programming languages that are supported with the Microsoft graph API something else that I haven't done yet is how we can get access token how we can interact with Microsoft graph API using Java which is also supported and also I have a new bit of experience using Java so there will be a video that will come to show you how you can interact with the graph API with uh Java and also more to come um using python Moto Cam using a Powershell and interacting focusing on interactive with Microsoft graph API emails SharePoint OneDrive name it so that is for today guys so do I like to subscribe for more soccer thank you for watching bye bye
Info
Channel: Bernard Mwanza
Views: 13,774
Rating: undefined out of 5
Keywords:
Id: C0hjEja-vPA
Channel Id: undefined
Length: 70min 20sec (4220 seconds)
Published: Fri Apr 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.