How to Send Emails Using ASP.NET Core Web API + Web Applications (Using SendGrid)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I will show you how to send emails using asp.net web applications this can be useful to reset user passwords to confirm orders and to confirm submitted contact forms so I will show you how to send emails using Centigrade so here let's type send grid let's go to the first link it is at centigrade.com then here we can click on pricing so here we can see that we can use send the grid for free to send up to 100 emails per day now we need to create an account in my case I have an account so let's click on sign in so after creating a new account we need to wait a few hours till the activation of this account and then we need to verify an email address that will be used to send the different emails so now let's verify a new email address so we need to click on settings then send their Authentication then here we need to click on verify a single Center then here we need to provide the details of the email address that we want to use in my case I have already verified an email address so this is the email address that I have already verified and it will be used to send the different emails now to use Centigrade with asp.net we need to click on email API then integration guide and because we want to use centigrid API we need to select web API so let's click on choose then let's select c-sharp and here we need to follow these steps so first let's create a new API key we can call it as the name of the asp.net web application for example I will call it best store API key so this is not the API key itself it is the name of the API key now let's click on create key so this is the API key that we need to use with our asp.net web application then we need to install send the grid package and then we can use this source code so here let's copy this method and then let's create a new class that will be used to send emails so this class will be used by other classes this means that it is a service and I can create it in the services folder so here in the services folder let's create a new class and you can call it email Center then here let's paste the source code that we have copied and here we can see that we have some errors this is because we need to install the send grid package so we can click on this button then install package Centigrade then find and install latest version then let's add the other required packages now let's initialize this variable with the value of the API key that we have created so let's copy this API key let's delete this text let's create a string and let's paste the value of the API key so this method is called execute let's rename it and let's call it send email it is a static method so this is not required we can keep it as an ordinary method so let's delete static and let's make it a public method so here we have the from variable it contains the email address that will be used to send the different emails so we have to replace this value by the email address that we have verified on send the grid then here we have the name of the sender we can remove this and replace it by the name of our application so here let's write best store for example then here we have the subject of the email it is a static value so instead of providing the subject as a static value we can provide it as a parameter to this method so let's delete subject from here and let's create a parameter called subject of type string then let's replace the destination email address by a variable so let's delete this and let's write two emails which is the destination email address now let's add this variable to the parameters of the method so here we need to add a string called to email and here we have the username so let's delete it and let's replace it by a variable called username let's add this variable to the parameters of the method and here we have the email message so let's replace this static value by a variable that we have to provide to this method let's call it message now let's add this variable to the parameters of the method so just here let's add a string called message also we don't have a HTML version of this message so here we can keep this variable empty now we can save this file and we can use it to send emails so let's go to our controller it is contacts controller and it is available in the controllers folder and when the user creates a new contact after registering the contact in the database we need to send a confirmation email to the user so just here after adding the contact to the database we need to send a confirmation email then let's create a variable that will contain the email subject we can call it email subject because we already created a variable called subject so it is a string let's call it email subject and it is equal to contact confirmation for example the second parameter required by this method is the destination email address so the destination email address is already available in contact dto.mail then we need the username so the username will be the first name plus the last name so here let's create a variable of type string called username the last parameter is the message that we want to send so let's create another variable which is the email message it is of type string let's call it email message so here we have D at the username then a confirmation that we have received the message and here we can see that we will also provide the message that is submitted by the user so we have to add the message that is available in contact dto.message now let's create an object of type email sender and let's call the method send email then let's provide this method with the required parameters so we need to provide the email subject the email address the username and the message itself so this method is of type asynchronous method for this reason we need to wait till the end of the execution of this method so here we have to add dot to 8. now let's test the application let's create a new contact then let's fill this object then for the email address I will use my email address to be able to receive the confirmation email so this email address is the sender email address but also it will be the receiver email address now the phone number is optional so it can be empty let's provide a valid subject ID one for example then let's provide a message let's click on execute so here we can see that we have a new contact now let's take a look on my email account and here we can see that I received this email address so this is the subject this is the name of the sender which is the name of our asp.net application here we have the first part of the message and this is the message submitted by the user
Info
Channel: BoostMyTool
Views: 7,996
Rating: undefined out of 5
Keywords:
Id: UCcSlfCK2Uw
Channel Id: undefined
Length: 9min 48sec (588 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.