Send Email with a .NET 6 Web API using Mailkit & SMTP πŸ“§

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there thank you so much for clicking on my video my name is patrick god yes it is really my last name i know some of you were asking if it is but it really is god god yes this is my last name but anyways today we are going to send emails with dot net 6 with a net 6 web api and that's essentially already it right so we are going to create a web api a project with net 6 and the only thing we want to do there is send an email and we will use the nuget package mail kit for that because microsoft even microsoft is saying that guys please don't use our smtp implementations use mailkit that's better and it really is it is really easy and simple to do that so i would say before we start if you like this video and learn something i would really appreciate it if you click the like button and maybe even subscribe to my channel don't forget to hit the bell icon here because then you get a notification when a new tutorial is online additionally maybe you want to subscribe to my newsletter for these videos here earlier in your inbox and also access to upcoming online courses like the.net web developer bootcamp where we cover the backend with the web api entry framework core a database of course and also the front end with blazer web assembly and also some stuff like gits and agile web development so scrum a little bit maybe devops stuff like that if you're interested in this then maybe the newsletter is something for you thank you very much for considering and the last thing as always thank you so so much for all your support guys toddler is asleep i'm making these videos and it's only done with coffee this is the spirit but okay i gotta be honest sometimes it's even tea yes sometimes you can stay awake with tea like today this is t okay so now i think i'm done let's send an email now with the web api and.net 6. enjoy the tutorial all right here we are visual studio 2022 the first thing we have to do is well create a new project i guess we wanna use an asp.net core web api of course you can also already use a blazer webassembly app and use your email service with that but let's keep it simple here and we call this simple email app great name dotnet six it is we leave everything as it is as well and hit create and then the very next step when the project has been created is installing a nuget package and that would be mail kit right because this is the way to go even microsoft suggests to use mail kit so right click your project manage nuget packages go to browse and then we have a look mail kit it is and that's the one 41 million downloads so far not not it's not net core make it it's not abp make it not that stuff here simply make it by jeffrey steadfast and open source.net male client library for windows mac linux and mobile platforms such as ios and android so please insert this thing and then a click click click click okay and it should be here alright so let's have a quick look yep make it is installed and that's that's in essence everything you need right so now let's already create a controller real quick and dirty and we call this thing it's an empty api controller make sure to use the named the empty api controller not an mbc controller or anything like that and then let's call this email controller and the thing is what we want to use now is ethereal and if you don't know what that is etherreal.email it is here this is a test smtp service as you can see here a fake smtp service and well we can create this to test our email service now so let's just create an ethereal account for instance and now we've got elijah kwiksen so that's the account we can use now great so let's do that i would say and before we can do that first let's create a method so for instance we just create a post method with the request method post and as an argument we use the body so we hard code the recipient and the sender and the subject but the body should come as an argument again this is the first quick and dirty way to do this and i think for most of you this is already sufficient and then you know how to implement your email service so this is a public eye action result method we call this sent email for instance and again we add the body here as an argument and now the first thing here is our actual email and this is a new mime message and you already saw the namespace so with control period now we add try to add using mime kit so this is the namespace we need maybe we can close this here all right nice and now the next thing in essence we just have to add some parameters here from add mailbox address and then we parse and now we need the email address from ethereal that's the one okay we go back copy this here [Music] and then two let's just send it back i would say email to and then again add a mailbox address parse this all right this looks good then the subject so email subject would be test email subject and then the body so email body and here we say new text part text formats is html in our case text format and let me just add the using mime kit text so this reference here and this is important now just short note we can use html or plain text for instance or rich text whatever you want i think html is okay and in that case you can use your htm tml tags for a header for instance or whatever you want to use and the actual text then is now the body parameter this argument here for this method so this is now our email our email object and the next part now is to send this thing so we're using an smtp client here smtp and this is now very important pay attention here there is an smtp client and when we try to use a reference here it is not system net mail please use mailkids.net.smtp even microsoft says please use mailkits and not the old system netmail so mailchit.net.smtp it is and now we can use sm cp if you write it properly connect and in here now we need the actual server and here you can see it smtp ethereal email all right so let's just copy this now the thing is if you want to use a real smtp service like gmail for instance it looks a bit different of course so in that case that would be smtp gmail.com or you've got hotmail that would be smtp live.com or office 365 in that case that would be office 365.com and i'm pretty sure there are lots and lots of others so please look these up if you want to use something else but now after that we've got the uh port again let me have a quick look see the port here so 587 it is and now here we are using secure socket options at the reference again and then start tls as it says here elevates the connection to use tls encryption immediately after reading the greeting and capabilities of the server if the server does not support the star tls extension then the connection will fail and the not supported exception will be thrown okay so that's the connect method then authentication smtp authenticate now this is the username and password so in this case this is our username and the password is this thing here all right and now we can already send our email object and also disconnect and yes we set this to true all right and in the end we simply return okay so that's actually everything and i would say we test this already so we run our app and can then use swagger ui to test our email service there we are and let me just open ethereal here and put it there all right great and now we open the mailbox for this account now this is the first ethereal test mail so this is not sent by us but now when we add a body here so try this out and let's just say h1 this is a test like that we hit execute we get an okay back that's nice and now let's refresh our mailbox hey there's the email test email subject we can go here and also see this html email and also the plain text if we want so this works pretty pretty great i'd say now this might already be enough for you so in essence you can stop the video already but if you i don't know use a better practice here for instance uh we can do that as well of course so i would say we now create a service for sending this email maybe also a dto data transfer object where we add in at least the the to field the subject and the body as well and maybe we can put the username and the password in the app settings right so let's do that i'd say first service we create a new folder and i'd like to call this thing services and then also another folder which is then the email service and in here we create an interface all right call this i email yeah i email service and also in an implementation class which is then the email service using the i email service interface yep let's rebuild and deploy and also let's add this here and register it as a service services at scoped i email service email service and we add the using directive make it global because we need this in our controller as well and we write another method here void sent email and now in here let's also add the dto so maybe add another folder call this models and then add another class email dto maybe and this gets 2 which is a string empty also subjects which is also an empty string and then of course the body all right oh okay and now let's use it here email dto we call this request for instance like that using this and let's put this to our program cs okay save everything got an error where is it yeah that's absolutely true so we implement this thing here now and in essence we can copy this paste it here and now we say of course again we need all the name spaces all the references again make it.net smtp it is and the last one here all right so now two comes from our request object all right same for the subject and of course the actual body all right that's one thing and now we've got the username the password and maybe also the the host the smtp server in essence so let's add this to the app settings json file so down here we simply add say email host and then email user name and then email password okay and then we copy this first the host a username and last thing now the password and now to access this we need our configuration so we add a constructor here and inject the eye configuration call this config maybe create and assign the field add an underscore here and now here we can say config get section and what's the name email host and from that we want to get the value in essence we do the same here for the username and the password so email username it is and email password let's double check username with a capital n let's change that okay i think that's bad so of course you could have also added another section and then nest all the information so you could call this email config for instance like here in the logging section and then just use the host the username and the password name for these strings instead of email host email username email password and so on this is totally up to you but i think for this quick way here and just for learning purposes you get the idea and let's also yeah let's also add it here so we've got the um the from value is then also the username all right so this is now our send email method and the last step now is to change the controller essence and here we need another constructor of course and inject our email service so i email service email service initialize it at the underscore here as well and then we just say email service send email with now here the email dto requests and here also the request and that's it okay we save everything and test this there we are got our email dto here and now this looks a bit different but again let's just send it to this guy elijah quittin uh that's the password so that's the email great great party yesterday maybe and the body i drank way too much all right okay let's hit execute all right 200 okay let's open the the mailbox and as we can see great party yesterday and i drank way too much and in italic great and that's already it let me push this to github real quick simple email app it is not a private repository let's make this public create and push and then have a look at the video description below to get this repository and that's it i hope you now know how to send an email with netsix web api pretty easy right i love it personally really and i will consider sending more emails in my projects in my applications in particular for error reporting for instance so if something is happening that was not expected and an exception is thrown then maybe we want to send an email grab some data and then we can fix this stuff so for that i think this is a great opportunity also tracking maybe but depending on the country it's not really allowed i guess but this is a completely different topic so i hope you learned something and if so i would really appreciate it if you click the like button maybe subscribe to my channel don't forget the bell icon to get a notification for a new video additionally again the newsletter might be something for you for the upcoming.net web developer bootcamp and last not least thanks so much for the tea or the coffee whatever it is i love you guys forever thank you so much for your support and now if you're interested in some other videos on my channel please have a look here on the side then just click on the thumbnail you're interested in enjoy the next tutorial and then the next tutorial in the next tutorial and i think then you know lots and lots of stuff regarding.net and laser maybe all right so now thank you so much for watching thank you so much for your time and i hope i see you next time take care
Info
Channel: Patrick God
Views: 49,385
Rating: undefined out of 5
Keywords:
Id: PvO_1T0FS_A
Channel Id: undefined
Length: 22min 1sec (1321 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.