Send mail from Dot NET Core Web API with attachments | .NET 7.0 mail sending using mailkit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to my channel here at a case and in this video Let Me Explain how to send mail from our dotnet core baby APA with below scenarios so the first scenario is very simple sending plain text messages and the second one is sending mail with an HTML content it includes some images and links and the third one is sending mail with an attachment and the file loan is sending mail with multiple attachment files okay here the first step is we have to create the web AP application I am already having one existing application I am going to reuse the same and my application version is.net 7 if you have.net6 that also fine and the next step is we have to install the packages mime kit and also mail kit so in our application right click and manage nuget packages the first one okay this installed and the second one is mail kit okay so if you are using visual studio code uh the same packages you have to install through our command line prompt okay so let me reboot our application and the next thing is we can create the required files I'm going to have one helper folder and here we can create one CS file mail request here we can have some properties that actually we are using in our mail and the second one is subject in the same way I'm going to create one more CS file that is for the email settings here we can have the properties of from email okay password host display name and the final only spot number okay our model classes are ready and the same properties we can declare in our app settings.json file so the first one is email so here you have to provide your from email okay and the second one is password here you have to provide your password and the next one is host the host SMTP gmail.com okay and the next turn is displaying here are the keys and the final one is port number we can use the Phi 87 okay so we can save this one and also let me configure this one in our middleware so then only we can use through the dependency injection Builder dot services configure email settings again this Builder Dot configuration get a section of here we have to provide our section name the section actually the section is email settings okay and the next thing is I am going to create the email service so insert the service folder I'm going to create one interface first I email service okay and also I'm going to have the implementation class also so here we can Implement that interface I email service and also we have to include in our middleware section Builder dot services so here we have to include our interface and also the implementation class so the basic configuration we have done now let's start our implementation first in our interface let me Define one one function so the function name is in the email distance synchronous function so we can do like this task and the input is we already having one mail request class email request so next in our implementation class okay and here we can have one Constructor and let me inject our email settings private read only email settings next to this Dot email settings equal to options dot value okay this is fine and in our send email asynchronous function let me initiate our my message so here we can include all the properties one by one so the first one is sender Center is nothing but from email only so that actually available in our email settings okay and the next one is to email so the step is same like um mailbox address dot parse so the two email available in the mail request okay so in our example I am just using one to email so in case if you have the multiple or two email so in this place we can generate one for Loop so we can add all the emails only one okay and the next thing is subject so subject also coming from our mail request and the next thing is body so before that let me declare one Builder okay I mean the bodybuilder class so here so in this Builder dot HTML body so here we can include our email request.body okay I'm mail request and finally this email dot body equal to Dot to a message body okay so now we have the sender and two mail subject and finally we have this body also and if there isn't any attachment that actually we can include in this place so before that we can complete the configuration for sender mail so once it is working fine we can include the attachment related changes okay for sending the mail we are going to use the asymptic land there's some tip equal there is some typical lines so first we have to connect our SMTP client so the inputs are so the first one is host and the second one is port number so finally we can include the secure circuit options okay so that is the start TLS next we can add the indicate our SMTP using our credentials so the first one is username so username is nothing but our from email settings dot email and then our password once it is authenticated we can send an mail so it is the synchronous function so we can use this sub weight SMTP dot send a synchronous function okay so here we have to pass our email and finally we have to disconnect our SMTP so in the service side we have completed our changes now let me go to our control section so here I already am having one customer control so let me use the same thing so let me create one action method and it is the post method so let me initiate our mail request class here so mail request a DOT gmail.com so next to mail request dot subject welcome to Mahir attackies and in the body I am providing on client text okay thanks for so we can move this into the double quotes okay and the next thing is I'm going to inject our email Services here private read-only I email service the same thing we can inject it here so this dot email service equal to service so here it is the synchronous method so we can use the avoid and our mail service yeah this one email service dot send email asynchronous we can pass our mail request so here we can include try cash if there is any error we can capture in the sketch will get our exception just to throw it so in this app settings.json already I'm having this tested gmail.com and dummy password so here I am going to change my exact from email and also the password so now we can run our application and verify it so let me execute the send mail function okay see we are getting the exception application specific password required so what exactly I have done I provided my email ID and also the password so basically it will work exactly on your back okay so now the Google changed their security policy so we cannot directly used so instead of that we have to create the application password app password okay the second link so if you are new to use this page you have the option like that only so here first you have to choose the application so we are going to use for our mail only so let me choose this mail another extra device so we can choose the custom name as some TP mean finally you have to click this generate okay it will generate one key so instead of the email password you have to use this one change this password section I just included so we can send a mail once again see currently I'm getting this 200 response now let me go to my email see now I am getting message thanks for subscribing us okay so now instead of the plain text message let me send them HTML content the function actually we can use in our body section so whatever content here we have so let me cut everything so in this response side I am going to include one question tag so we can provide inside the HTML content now let me run our application once again we can send the mail once again okay response is 200 let me check from my mail see so now you can see the difference right thanks for subscribing us so initially it is the text now it is the HTML content for better understanding already I am having some kind of HTML content that includes our Channel logo and our membership joining link so let me replace with this content see this is very simple so inside the due section I am having the text welcome to Nigeria techies and this one for our logo and the known text thanks for subscribers and we have the link this is for joining the membership and finally my contact information okay so mostly if you are preparing this HTML content try to have the inline style so in case if you are referring additional class so that may not going to work so let me run the application once again so basically what I'm recommend just to make your HTML content in the HTML page once it is fine you have to include in our application okay so response is fine and mail also be received a c so completely blue background and the text we have this logo and the link please join membership by clicking the link so when I'm clicking this one see our membership joining English opened okay so anyway let me close this one So currently we have covered with the basic email and email with an HTML content so now let me focus on how to add the attachments okay for adding the attachments there are two ways we can include so the first thing is we can have some files okay in some other path that actually we can access from our application and address the attachment and another one is we can dynamically generate using our C sharp code then we have to attach in our mail in this video I am going to have one PDF file in our application one of the path so that actually we can attach from our email okay so first let me create one folder so in this folder I am going to include one file okay it's a dummy PDF file only next in our coding binder View first I am checking whether we have this file if we have the file only I'm going to attach first we can access this file using the file stream so here so the first input is for file path and then file mode dot open and file axis read okay now we have our file string next I'm going to use the memory string on top of that let me declare one bytes array file by it's equal to memory stream dot two array so once we have the bytes we can add us the attachment Builder Dot attachments dot add the first one is file name it is the PDF file add the next hour byte information so here we can harvest the file page and finally this content type so here we can provide the exact content type information so let me save this one your file name is PDF so the steps are very simple already I'm having file in my local folder I am just accessing using this file string then I am converting it to the byte so finally I am adding this bytext attachment so if you are generating the files from our c-sharp application obviously we will get a stuff byte format only initially so that actually we can directly add it here okay so let me run the application and we can verify whether it is working or not the response is 200 only you can check from our mail see we have this content okay the attachment is missing so let me verify once again so I understood the problem the reason is we are adding this attachment in the objective Builder okay so before that I am just included this body section okay it's supposed to move these two lines after our adding our attachment so then only it will work okay great the response is 200 see now the attachment was there the content is coming and also we have this dummy PDF file so the same procedure for attaching multiple PDF files also So currently I'm not going to include one more file so we can use the same stream I mean this by data just to change the attachment file name online okay I hope it will work see having two attachment the first file name is attachment and the second one is attachment to and also we have the content also okay so now we almost covered all the scenario so in this example I just use this two only so in this real time scenario we may have the CC and BCC also for adding the BCC and the CC we have the option there the same see email.cc then add then you have to follow the same step the similar way buffer the BCC also the same okay so now we closed this video so still if you have any doubts or clarification please post in the comment box and also please don't forget to subscribe my channel thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 9,560
Rating: undefined out of 5
Keywords: how to send mail from dot net core web api, send mail from dot net 6, send mail with html content dot net core web api, sending mail with attachments in dot net core web api, how to create application password in google, send mail from dotnet core using smtp, send multiple attachments from dot net core web api, send mail using mailkit, send mail using mimekit
Id: 0e325O9mzP8
Channel Id: undefined
Length: 24min 19sec (1459 seconds)
Published: Mon Apr 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.