How To Send Email with Attachment in ASP.NET MVC application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to digital Tech join I hope everybody is doing good and you guys are really enjoying the educational videos that are uploaded in this channel in this video tutorial I'm going to show you how you can send an email with an attachment using SMTP details that you have received while you have purchased a hosting plan from any of the custom domain providers now when I say a domain registration providers means I mean to say like host Qatar like you know there is host getter and these are the people who provide the domain name as well as hosting with an email plan then there is something called name CH so these even they provide you with a domain name and and hosting plan then there is obviously go ID so uh the these are some of the examples that I'm listing out now this all providers um provide you to buy a domain and along with the domain they also help you to buy the hosting which comes within also a plan for emails now when you want to send an email they also provide you with a plan and they also provide you with SMTP details now we are going to use those SMTP details and send an email from our application now typically all these domain providers or hosting providers provide a standard SMTP details that I'll cover while I am doing a practical session of sending email with an attachment using asp.net MBC application so guys let's uh jump into to our Visual Studio 2022 I'm using visual studio 2022 with a preview version preview 2.0 which is the latest one so here I'm going to create new project and select MVC application send email okay this is a demo this is a solution name and the project name I'll just untake configure for https I'm selecting net version 8.0 and I'm clicking on create cre button so this is a project loaded and um we are going to create a simple form where we can enter the from date to date subject line and then the mail body along with the option to select attachment before we proceed we will create a model class just to pass information between our view to controller so uh let me right click on the model and say add a class called email entity okay and here I'll have some basic properties defined so the first one will be string and this will be from email address and I'm going to copy this and this will be two email address and then subject line and body and the URL so this will be our email subject or you can also write subject and then this will be our email body message this will be a URL so here we we have attachment we can say attachment URL and this will be not compulsory so here I'm just going to say validate never because email attachment can be optional so this is the property I have defined now let let create the view so we are going to use a default view so inside under the views folder there is home and under home there is index uh similarly for we have the default home controller so we are going to use the default and create the form so here I'm going to double click on index and I remove this first I'm going to use email entity okay and I'm going to create a simple form that will enable a user to enter the values so I'll create a div class okay and this will be a card and Shadow border zero this will be a main div and then inside the main div we'll have one div for title card Eder and this will be gray color and this will be I'm adding a griding effect to it inside this we'll have one more D class and this will be what row sorry this will be a row and inside that we will have D class which should be called 12 and text Center and here it will be H2 send email okay so this is an edor so this becomes our first d uh and then inside this we will have one more D and this will actually have the form Fields card this will be our body with padding two okay now inside this we going to have ADD our controls so this will be form method post and here inside that we going to add div class call 12 and inside that we going to have a label which will say from email and input ASP for we are going to take the and this will be from email address and class form [Music] control okay so this is the first field and then we want to copy this and second and third so this will be from and this will be two email address this will be our subject line this will be our mail body message body email message body so this complete the form and now we will actually add the buttons to it and we need to also add an attachment so I'll just add one more tag for attachment we just copy paste and this will be select attachment or select file this will be attachment URL but this will not be an input this will be type file so this will be your input this will be type and this will be file and it will also have a carrier name which will be used while we are fetching this file which will be I'm calling it as a posted file and after that I'm going to add a br tag and then add the buttons so this will be another row and then we will have div class we'll divide it into two section six and this will have input button type submit this would be class BTN BTN primary this will say send email okay now and we can have one more button which will say cancel or reset or something like that so we'll work on this button later because as of now this is not important so we'll just won't add anything over here let's run this project and see the form here you can see a simple form is there we have from email address from email to email so I think so I just not changed I'm just going back to the design this is from email this is to email this is your subject Check Line and then this is your message okay and then this is Select file this I'm just clicking on the a Reload over here and then coming back to our form and we should be able to see so you are able to see the updated one okay uh so if you click on this you can it see that you're able to also select the file as well now uh what I also noticed that email subject message body is looking like a text box because obviously I've taken in text box so it should be a text area so that we'll be able to enter more message in our body right now when you run the project you see that you're getting a single row to enter your message which is generally not the case let us modify the content so here we come to the message body and in just change the input to text area and now when we say text area then we also have to specify the rows and columns so row is five and calls is around 20 so let's run the project now okay so there's some issue let me just check it should not be displaying this let press go to design view uh text area message rows column let's click on UT reload and refresh yes so since it's a text area we need a closing tag as well uh separately so I did that and now we are able to see it's properly displaying email to subject message now you can enter the and select the body so this is a very simple form we have designed now let's start coding and actually implement the actual code so that we able to send the email so here what we're going to do is that as I mentioned whenever you register any domain and buy the hosting you can also buy the uh email plan and once you buy the email plan you will get certain uh parameters that you can use that into the application to send an email so what I'm going to do is that I'm going to go to app setting and first configure those required parameters in my app setting which I'll retrieve in my code to dynamically fetch it from the app setting and send an email so let's create a configuration uh setting called email config so this is my email config and this will have the following settings so we typically uh need your host so this will call it old host and then this will have the host name so these values depend upon your like mail your host name.com so when I say your host means when you buy a domain for example you buy a domain saying um my domain.com so your site is registered as mydomain.com so wwyd domain.com your page opens so your my domain is your host name so typically most of the time whenever you buy a hosting and according to you buy a email plan your host will be always mail do your host name that is my domain.com but this can vary so this can if you go to Cod ID they might give you a separate know host name for sending email or if you go to a name CHP they can give you separate email or if you go to osat you can you can give so you can fetch all this information from your C panel that is your control panel wherever you are buying the hosting okay so here is my hosting this is a dummy hosting I'll replace this once my I will show you how the mail goes then we require the port so typically this will be my port I say this will be 26 so is it will be my port number and then after that we need username obviously every SMTP uh needs an username and password so this uh so this is my username my host username and then finally we'll also need password so this will be my my hosting password okay so these are the information required typically uh for sending SMTP no sending a mail using SMTP TP so let me just retrieve this this parameters into my code so let's go to home controller and let's create a method called send email public I actionable send email and now this email will take email entity because we are passing OBG email par parameters and then since we it will also upload files I need to add iform file and this is the PO Ed file so this is a uh mail that I'm going to send it and this will return View and this will go back to our index so first thing we going to do is that retrieve those parameters from app settings so in order to fetch the values I'm going to Define a variable called where my my app config and here I'm going to say new configuration Builder new configuration Builder and Dot add Jason new configuration binder it's Builder it's not binder it's Builder Okay add Json file and here I'm going to name my app setting Json file so here just click it F2 and then come back over here and paste it so that you ensure that you're not making any mistake Json this is a Json file and [Music] build so now my app config represents the entire app Jon file now once this is done I can declare the variables and fetch their values so I say our username and this will be my my app config do get value string here you need to specify your first label so you say email config and then call the username so you go back to your app setting you can see email config is the main addor and inside that we have this parameters that is the reason I mentioned email config and then the username similarly I'll fetch other parameters like host password and host and the username so here I'm going to say first I fetch the username then I fetch the password let me keep username Capital username and this will be password and this will be Port host sorry and here we'll have host and then we'll have P so let's run this application now in order to run it what I going to go going to do is that go to our index razor page and here we'll have to mention the action so I'm say as action this be send email also one more thing you need to remember that since we are sending email so view has to be uh able to U post the attachment in order to work with attachments in the view you'll have to also add tag called ENT type and then say multiart form data this is very important because if you don't add this tag whenever you're selecting a file and posting it to the controller the controller won't have the file that you have posted so first is a action now we be redirected to this action when you submit the send email so let's first check whether we are able to fet the records from the app setting so we are getting an error over here let me just say app setting Jason just let's close this click on home configuration a new configuration Builder Json and app settings which looks good let us run the application once again and see what exactly the error is saying just expand this and it says inner exception is AB okay I think so there is some mistake in the Json f app setting Json file and here we have so here you can see this is a indication yes a comma is missing that is why we facing that issue we need to add a comma over here that was the main issue let's and this time I think so it should work definitely yeah now uh what we'll do is just add values it's not validated so it can be anything right now and let's click any attachment and let's click on send email now this action is called send email and here first we defining the adjacent file the object is created the t is uh sessions now first I'm fetching the username so let me just say yes it is fetched my host name and then we are setting the password and then the host name and then finally the port so everything is getting fetched now from a app jessing file in in the code so now let's write the code actual code to send an email double click on on the home controller now once we have all fhed all the values so here all the values are getting fesh from here so once we have the values over here let's actually try to um create a mail object and send the mail so we need to say mail [Music] message mail message and this will be a message object okay now this message object that is message. from address and this is already fetched so you can also specify the form address in your app setting because form address is will be fixed so here I'm going to write also mention the form address so this is from email and here I'm going to mention the actual email from where the mail will go so it will be contact atate digital Tech join.com this is the actual email address from where the will actually go so let's go to the control and from email and F this so we F this and now we going to supply that over here then message to so this we'll get from the email entity object so email property and this is 2 address and this will be two string [Music] okay so this is not equals to we can assign it so basically we'll have to pass it this [Music] way there is still some message from add equals to new mail address and then we add it so this is how we assign the property similarly two will be add so here it will be add and then we have pass this parameters okay similarly we'll have subject message do subject and this will be parameter to subject then we'll have something as a type that is body HTML and we say through it's an HTML format then we'll have the actual message body do equals to OBG email do message body finally we need to add the data attachment so for attachment we will say message. attachments do add and say new new attachments and we are getting the value from this variable posted High form file posted file so we going to say posted file do opens lead stream and the file name so posted file file name so this is creating a stream object and and adding that to the attachment now here after this once the values and everything are set we are actually going to call the SMTP details and send the mails for that we going to use TR catch to to catch this exception so this will be a catch and this will be an exception and then we'll also have to have a find finally tag for clearing up the resources if any as a good practice finally and we say so this is a structure for TR here inside that we are going to actually call SMTP client this will be our male [Music] client okay and here SMTP client will take an OST so we'll pass the host variable which is already there captured over here so this is the sm2 client that is first been called host does not exist because we actually so I'm going to copy this try and push it over [Music] here so sorry we don't have to do that that was [Music] a this is the right way sorry so this is the host name so we have passed the host name to it now we want to say this is a typical standard code that will be used across so we are going to say main client do use default cenal in my case it is false after that I'm going to say mail client do credentials so new [Music] system.net network credentials and here I'm going to pass the username password which is obviously captured here username password okay after that it will ask for the host so basically we need to provide the host so I'll write the host over here then mail client do port and I'll be providing the port and finally we going to say mail client do send and we going to pass the message object here this is a message object over here and we'll use the finally object and we dispose over here irrespectively if there is an error or not we're going to use dispose and close this now you know order to use finally your scope of variable of Main Line it is should be above the try object to here I need to declare the variable over here so that I can use it over here so here I can use M line dispose you see you have to do that and here it's giving an error because Port is a string so here I'm going to change it to integer so this should work now yes so this is work so this is the base of a code for sending the mail sometimes depending upon from where you have taken the hosting sometimes you might have to pass enable SSL equals to through or false in my case I'm not going to pass the variable so it I'll just comment it just I want to tell you so sometimes you need to do the toggling in some cases you need to use use default credentials to through so that that M mentation and Clause that you need to manage basis from where you have purchased your domain and hosting so this is the code that I've written now I'll just show the code once again and then change the values to the actual values before I show you how the mail will be sent so here I just run the application now I'm going to write so basically we also don't need the from email address because we are actually from email address will be always constant so in that case what I'm going to do is that remove this from email address from here and in my entity class I'll just make sure that um validate never is taged to it so that we don't face any error and now so now you can see two emails so I'm going to say digital Tech join at gmail.com uh this is my subject hello hello subscribers and here I'm going to say a file I'm going to say image file and click on send so now let us go through each and so first we Define the app wrting object now we F the username password host port and the email then we create a mail message object where we pass the from address so my from address is contact digital Tech joint.com then the email address which is passed from the razor page uh view that is digital Tech join gmail.com then the subject uh and the ma message body is all passed here it is attaching the um the attachment that user selected after that I Define the smt be client object now client object takes use use default credential FS this is the username password that will be Pa this is the host and this is the port number 26 and finally when I click on send it will send the message in order to send the message I will have to enter the valid credential over here and then I'll run the code again guys I've updated the actual um settings and usern name password in my app setting I'm going to run the code now and we are going to actually send a email I just run it again okay so I'm going to add the actual values digital TJ gmail.com I sending video on sending email with attachment custom SMTP details please do subscribe and like our videos this is my subject here I'm going to select an attachment file so I'm going to select an image and now I'm going to click on send so this will have all the values and now I'm going to proceed and click on send you see it successfully sent now once it goes to the finally tag it will actually dispose the email object and then it will return back to our index so here I'm not showing your that email is sent successfully that I'll show to you but before that let's go go to our mailbox and see whether we have received the email guys you can see contact video on sending email with attachment successfully come into my inbox and I'm able to see that image and also see able to see the message okay and from where the mail has come so I've successfully sent the mail it is come to my inbox and our code is working now now we'll just add one small piece of code to inform user that the mail is successfully sent so what we're going to do is that we're going to go to the controller and once the mail is sent we're going to create a viewback viewback do message and you're going to say email sent successfully so this is a confirmation message and here if there is any failure we need to capture those failures over here and say that you know exception we put through through that exception also you can log that exception in your in your in your file and uh in my previous video If you go to my channel over here Ive already added one video which shows you how to log exception in your text file so you can use the same video and write the code over here in the exception block so that you can see that exception uh in your text file here for now I'm going to just say failed sending email okay so this this is how you need to say and now then we simply call this in our view so here I'm going to write just mention one tag div and I'm just going to put atate sign and print that message over here and you can just change the formatting and see S3 yeah so that's it so let's run the project now again so I'm going to send the mail again this time I message body and I going to select attach attachment and click on send I'm going to remove the break point and in the application here you can see email sent successfully okay so this is how uh we can use our code SMTP code and uh do net provided libraries to send email using your custom hosting or domain plan that is pro provided by your hosting providers so with this we complete the session pleas uh guys please do subscribe and uh like my Channel please do share the channel link with your friend family and colleagues and help us to grow our digital Tech joint Channel thank you so much for your time
Info
Channel: Digital TechJoint
Views: 3,106
Rating: undefined out of 5
Keywords: aspnet, asp.net, aspnetcore, aspnetmvc, mvc, core, visualstudio, sending email in asp.net, send email, code for sending email with attachments, system.net.mail, vs2022 preview, vs2022, razorpages, course, learn, web development using asp.net, web developer, hosting, hosting smtp, smtp details by hosting providers, learn asp.net, programming tricks & tips, learn dotnet, dotnet, entityframework, dotnetconf, dotnet framework, microsoft visual studio, coding, web, email
Id: qdsysLAhrrg
Channel Id: undefined
Length: 35min 51sec (2151 seconds)
Published: Sun Oct 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.