Send Emails with Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys this is Junior here and today I'm gonna show you how to send emails using spring boot almost every application you use if they collect information about you after your name it's gonna be your email address that they're gonna ask if you think about your banking application your social media accounts your school all this application have your email address and it's one of the most common ways of communicating in this world of the internet so knowing how to send emails to your customers or clients is crucial to any application and this is what I'm going to be showing you how to do today using spring boot I'm gonna put this into context and what I mean by that is we're gonna imagine that we have users coming into your application and they sign up with their email address and we want to send them an email so that they can confirm their account or email address so we'll be building a small macro service where users can sign up or register for a new account and then we will send them an email to confirm or verify their new account or email address I'm gonna teach you a few different ways that you can send emails so I'm gonna show you how to send Simple Text emails and then I'm gonna show you how to send emails with attachment then I'm going to show you how to send embedded files in your emails say you wanted to make sure that once the email is open the file you sent with the emails can be seen immediately so you want to embed the files in the email body itself so I'm going to show you how to do that and then lastly I'm going to show you how to send HTML email so these emails you get from your bank or your school that look like an HTML web page I'm going to show you how to send those as well and I'm gonna show you how to both add attachments to HTML emails and how to add embedded files in the HTML emails as well this course is really Hands-On this is literally the only presentation that there is in this course after that it's gonna be only coding so we have a lot to do let's go ahead and jump right into it so our spring initializer open and I've already filled out the project media data as you can see here it's gonna be Maven and I'm gonna be using Java and also using the latest version of spring boot as you can see here we have 3.1.1 packaging is a jar and I'm gonna be using Java 17. so now we're just going to go ahead and add some of the dependencies so obviously we're gonna need the web dependency so I'm going to add that I'm also going to need jpa so I'm going to add the spring data jpa I'm gonna add in a database in this case I'm gonna be using postgresql and also I'm going to add the mail so I'm going to type mail so we want the Java mail sender so I'm going to select that and then I want long box so I'm gonna say Lombard I'm going to add that in and lastly I'm gonna be using time Leaf so I'm gonna see thyme Leaf because we need this template rendering engine so that we can send the HTML email so I'm gonna select that so we have one two three four five six dependencies in this project it's gonna be a small project I just want to show you the demo of how you can send emails but we're just gonna make it like a mini project that you can actually just use and then expand upon so I'm gonna go ahead and click on the generate that's gonna download its zip file on my computer as you can see here and then I'm just gonna go ahead and open it up in until so I have the project open and IntelliJ and I'm just gonna go ahead and click on build and then build the project but at this point you shouldn't have any errors or anything the project should build all the dependencies should be installed or if that didn't happen then whenever you run the application they will just install because IntelliJ will kick in this process but you can set the bottom left corner here it says build successfully in four seconds and I have this new Target folder that was created in my application okay so we know that everything is good and the first thing I'm gonna do I'm gonna go inside of the application and I'm gonna rename this file so I'm going to do refactor rename and I'm just going to name it application so I'm going to delete that and then refactor I'm gonna select all and then click ok so now this is refactored so now the name of the main application class is just application so everything is good in the neighborhood what I'm gonna to do next is to start working on the user because you're going to assume that we have users coming in and they're going to sign up and then we're going to send them an email so just remember that the focus is going to be on sending the emails and not unlike the user itself but since we want to put this into context then I'm just gonna you know create the users and pretending that they're signing up for something and then we're just gonna send them the email upon signing up so let's go ahead and do that I'm gonna go ahead and right click on the main package name and then do new and we want to do another package so that's going to be the domain oops so domain obviously this can be model or whatever other name that you want and then inside of there is where we're going to create the new user so we're gonna say you know class and then I'm gonna say user press enter I'm gonna collapse this and we're gonna work on the user so the first thing we need to give it an ID because we're gonna be using spring data jpa also like to make this private okay so we're gonna have the ID and then we're gonna have the name so I'm gonna keep this very simple so you're gonna see string name and then obviously we need the email so we're gonna capture another string for the email and then we want maybe like a password just to make it make sense obviously we don't need these things for the emails that we're going to be sending and then lastly we can do uh let's do a Boolean and that's gonna be ease enable Okay so we're gonna assume that when the users sign up they're not enabled so we're gonna make this Boolean false and then once they open their email and then click on the link that we're gonna send them then we're gonna make this Boolean true which means now they can access our application or they're enabled as a user or client so this is the user class I'm gonna pass in some annotation so we can do maybe all our arcs Constructor so since we have lumbar we should be able to see that and then I'm gonna pass in no arcs Constructor I might not use all these Constructors I'm just going to put them in anyway and then I want the super Builder and we can pass in a name to this Builder by the way um wanted to show you guys this so if I go inside of this annotation you can see that for the Builder and the build name that you're gonna call to create a new object you can change those name as well by passing in the Builder method name and the build method name you just override these two values but I don't need to do that okay and then after that I'm gonna add some Getters and sitters so I'm gonna say sitter and then I'm gonna add some getter and then I'm gonna pass in the at entity because we want GPA to manage this class for us so I'm gonna add the at entity and then lastly I want to change the name of this table so I'm gonna do at table and then we're gonna pass in a name so we're gonna say the name of the table is going to be uh we're going to name it users okay so the name of the table when jpa creates the table for us is going to be users I'm going to scroll up some more and then we're gonna denote this as the primary key so we're gonna pass in the ID on this and also gonna pass in the uh generated value and then we're gonna do strategy and then just pass in Auto maybe okay so that should do it this is just an example obviously if we were going to create an actual user for a real application then we would have to think this through and then probably Define an entire sequence for the ID but that should be okay for the purpose of this course so this is the user very simple and very clean just for our demo and what we need to do now is to create another class that's going to be the confirmation so whenever a user create a new account we're going to create a confirmation for them like some kind of an object with the user ID and then we're going to assign them some kind of a key or some kind of a token which is why we're gonna send them in their email so that whenever they you know click on the link that we're gonna send them in the email then we're gonna enable their account turn this value to true it's not going to make sense as we work through the code so let's go ahead and work on this confirmation so I'm gonna go back and I'm gonna right click again create a new class and I'm going to name this confirmation and in here well let me copy some of the stuff that I have in here already and let's go back here I'm gonna paste this in import everything and also I'm gonna copy the ID because I know these are going to be the same so I'm going to paste that in there just like that okay so now I have um I don't want the all arcs Constructor so I'm gonna get rid of that I don't want the Builder so I'm gonna get rid of that and I want the new RX Constructor I want the Setters and the Getters and this is going to be an entity the table name is going to be confirmations I like to have my table names in plural so that's why I'm overriding the table names but this annotation is not really necessary and then I'm gonna do Ctrl alt o to clean up all the import and also rearrange the import and then I'm going to collapse okay so let's scroll down here the next thing I need is the token I'm just gonna name it token you can name it a key or something so this is going to be what we're gonna be sending to the user as their confirmation so this is the key that they have to give to us so that we can look their information up in the database and then confirm that this is really who they are and then we can pass in maybe a date so the date that we created the confirmation so we're gonna say local date time maybe and then pass in the created date okay and then lastly we're gonna pass in the user so the user this confirmation is four so and here we can save it's gonna be of type user from the class that we just create and then we're gonna name it user the confirmation ID is going to be the same the token is gonna be just a token uh we can pass in some cool annotation on here I guess we can pass in maybe like uh temporal and then we can say this is going to be a temporal type and then we can pass in the timestamp just to denote that this is a timestamp and if you want you can add I think it's called created uh create a date yeah you can pass that in there as well and then I'm gonna put some more spaces and I'm gonna say for the user is going to be a one to one mapping because we want one confirmation to be for one user and then we can pass in the Target entity in this case that's going to be the user class and then we can say fetch and pass in the fetch type of eager and I'm going to scroll up a little more um what else we need we can add the user ID inside of this confirmation table that JP is going to create for us and the way you do this is to do I think it's join column and then we're going to pass in some properties in here so we can see nullable false thick false is the default but it's not going to hurt and then the name is going to be the user underscore ID so whenever jpa is going to create this confirmation table well it's going to name it confirmations it's going to add the user ID this field that is going to be created inside of this table so it's going to pass in the user ID as a foreign key that's going to be in the user stable okay so the table from this class and then the last thing that I need is a Constructor so I'm going to say public and that's going to be confirmation and this Constructor is going to take a user so whenever I'm going to create a new confirmation object I want to be able to pass in the user and I'll scroll up some more and then we're gonna set the user to equal this user and then we're gonna pass in the created date so we can say created date and then pass in the local date time so local date time that now and then we're gonna pass in the token so we're gonna say this that token this is going to be some randomly generated token so we're gonna say uuid that random and then turn it into a string and that's all we need to do so now whenever we are creating a new user in the application we can also create a new confirmation we're gonna pass it the user and then we're gonna set these values and then we're gonna save that no confirmation in the database what this is going to do is it's going to create a new confirmation Row in the table and then for the user ID as the foreign key it's going to use that user ID in the users table obviously this is just one approach but you can design this in different ways if you want to just create the tables yourself set the foreign keys and primary Keys yourself you can do that but we have GPS so we're going to be using jpa because JP is going to do all the heavy lifting for us we don't have to write any sqls or anything like that I like to write my own SQL queries and I also like to use GPS I mean it depends on complexity of the project but I like both of them so it's up to you what you want to use so we have the confirmation we have the user so we should be good to go to create some repositories so that we can start saving some of this stuff in a database so let's go ahead and do that I'm gonna go ahead and close everything uh close all tabs and then I'm gonna create another package so I'm going to right click and then go to new package and then we're gonna name this Repository and then in here I'm gonna have the user repository so Java class this is going to be the user Repository and I'm going to select interface press enter and then we're gonna extend the GPU repository so jpa repository pass in the user is the type and the primary key or the type of the primary key which is along and then I'm gonna import that user class that we just created okay and then I'm just gonna Define uh maybe two methods so the first one is gonna return the user it's gonna be fine by email ignore paste so we're gonna pass in an email and then it's gonna try to find the user for us so that's going to be the first method if you want or you probably should make this return on optional so that you can throw an exception but we're just gonna keep it simple and then the second one is going to return the Boolean and then I'm gonna call it exist uh exist we can do exist by email and then we're gonna pass in the email as well so we're gonna say string email so what this is gonna do it's gonna see if the user by that email exists and it's going to return true or false because it's a bully so that we know that if someone already exists in our application then we can't register them again so we'll throw an exception or the application will throw an exception because I'm not going to be handling exception in this course but the application itself will just throw an exception if we're trying to save a user that already exists or we'll just throw a runtime exception if that is true so that's something we can do as well so this is going to be the user repository and if you want you can pass in repository in here but I don't think this is required anymore it will still work it's not gonna hurt anything but it will work regardless even if you don't have this annotation of repository okay so that's all we have to do here I don't think we need anything else we're gonna create the confirmation repository so let's go ahead and do that I'm going to copy everything inside of this class and then I'm gonna close it and then I'm gonna right click on the repository do another new class we're gonna name this confirmation repository it's going to be an interface I'm gonna press enter and then I'm gonna just select everything and then paste I'm gonna rename this and I'm I'm just gonna see confirmation repository not gonna press enter so confirmation repository we're gonna pass the confirmation class in here instead of user and we're gonna try to import it as well and in here I need one method so this is going to return a confirmation so I'm gonna say it's gonna return a confirmation we're gonna say find by token so we're gonna give it the token and then it's gonna return us the confirmation and then we're gonna say string two okay and I'm gonna do Ctrl alt o to clean and rearrange the import so now we have the two repositories so the confirmation repository and the user repository so that means that we can persist the user and also the confirmation like any object that we create off of this class we can persist them in the database using jpa so now what we're going to do next is to just create a simple service which we'll be able to use in our controller or in our resource so let's go ahead and work on that right now I'm gonna close everything again and then I'm gonna right click on the main package do another package this time I'm going to call it service and then here we're gonna have the user service so no have a class and it's going to be user service well this is supposed to be an interface so I'm going to change this to interface so what we're gonna have here just gonna be really simple so we need one method that's going to return a user we're going to call it save user and then we're gonna give it the user obviously in this case you would probably pass in uh like a user dto request or something like that because you wouldn't use the domain class and the controller like you would have some kind of a way to map the requests coming in to the user in your domain because the user coming in from the request wouldn't have like all the fields that you have defined for example whenever we're gonna send a new user to be registered we only need these three right so we need the name the email and the password we don't need the enable because we're going to disable them by default and we don't need the ID because JP is going to take care of that for us and generate the idea as the primary key in the database so you would have another class like user dto request or something and then you would pass it in here in the service you would have something in the middle that's gonna like translate the user request to a user and then you would save it in the database but again I'm pretty sure most of you guys already know that but we're just going to keep this simple because this is not the main focus of the course I just want to have some context so that it can make sense and that's just going to open your mind as to when you might want to use emails in your application so we're gonna have one to save a user and also another one that's gonna return the Boolean that's gonna verify the token so we're gonna say verify token so the token that we're going to generate and saved in the database we're just gonna verify it whenever they give it back to us so that means when they click on the link and they're verifying their Newark and I'm talking about this uh if we're going to The Domain go into the confirmation we're going to be confirming this token right here okay so that's all we need for our service we're just gonna give this service one implementation and then after that we should be able to move on to do some email stuff so let's go ahead and give a simple implementation for this service I'm gonna go ahead and close everything again and then collapse the domain the repository and inside of the service I'm gonna add another package I'm gonna name it impl for implementation and then I'm gonna create the implementation for the service so Java class user service impl press enter and then we want to implement that service so user service and then we're gonna Define these methods Implement methods just two of them and then we're gonna press enter okay so now we can inject our repository so we're gonna say private final and I want the user Repository name it user repository and we also need the confirmation request or private final confirmation repository okay all right and then we're gonna do the dependency injection via the Constructor so first we need to denote this as a service and also pass in the required so we're gonna see we acquired arcs Constructor okay all right so our dependency injection is done I'm gonna collapse these Imports and scroll up a little more so all we have to do now is to use these repositories to save the user and then get the token and verify it so pretty standard stuff nothing really complex I'm assuming your application will be way more complex than that but at least it will give you like some context around sending the email so now let's go to the uh let's do if the user repository so we're going to call the user repository and then Exist by email and then we're gonna pass in the user that get email so if this is true then we're just gonna throw an exception so we'll see something like throw so a new runtime exception and then we can pass in a string say email already exist okay so we're not gonna try to add the user again because they're already edited in our database and then I'm going to put this on one line otherwise we're gonna call the user repository again they're just called save and then we're just gonna save the user in the database and also well before we save them in the database we need to disable them so we're going to see users that Set uh enable we're gonna set that to false just in case that this was true because we know that when they're when we're saving a new user they have to be disabled so we're gonna disable them save them so now the user is saved next we need to create a confirmation for them so that we can give them a token so here is where we're going to call the confirmation repository that uh actually before we call the repository we need to create a confirmation so we're going to say confirmation we're gonna name it confirmation equals a new confirmation so that's what we're gonna be passing in the user for the Constructor that we created so if I go in there you can see that we're calling this construct so we're going to give it the user and then it's going to set the user for the object that we're creating with instructor passing the date as of right now and then create this random token which we're gonna save all of this and the confirmation table also with the user ID which is this foreign key okay so hopefully this is going to make sense and we're gonna take a look at the database to see what that looks like from a database perspective so let's close this off now we have this new confirmation what we're going to do is we're going to save it so we're gonna say confirmation.save and then pass in the new confirmation so at this point we need to send the token to the user to their email right so we have all the information we need we have the user email as you can see here and then we have the confirmation on which the token exists so if I do that get token so you can see that the token exists okay so we need to send that token to the user so now what we're going to do is start working on the emails so here I'm going to put a simple to do so I'm going to say to do and then we're gonna say send email to user sweet token okay so that's what we need to do here I'm gonna put that in a comment oops just like that okay so that's gonna be what we will need to do at this point before we return and then after we return we can just return the user so we pass into put another space here so we have to send the email at this point okay so we save the user well we disable them and then we save them we create a new confirmation for that user by passing in this user into the Constructor we save the confirmation and then now we need to send the email to the user to their email address that we have access to here and then send them the token to their email address so that's all we have to do for the save we don't have anything we can do with the email yet so we're gonna do this once we're done with the user service implementation so now what we're going to do with the verify token so say they access their email and then they send another request with their token and we need to verify this token so that's what we're going to be doing right now so let's go ahead and work on that we're gonna do the confirmation so we're gonna say confirmation we're gonna get a new confirmation and we're gonna call the confirmation repository that find by token and then we're going to pass in the Tok okay so we get the token that the user is going to send to verify their new email or their account so we got the token we send that token to the database to fetch an entire confirmation by that token and then what we can do is to find the user that is associated with this confirmation so we're going to say user we're just gonna be the user we're going to set it equal to the user repository that and then we're going to call find by email ignore case and then we're gonna say from the confirmation that we got we're gonna get the user so we're gonna say get user that get email okay so we're gonna find the user this confirmation is associated with and then now we can do user that said enable um we're going to set this to true so we're gonna enable the user and then we're gonna see user repository dot save because we changed something on the user we have to save the user again so now the user is enabled in our application or in the database if you want to you can do something like confirmation repository dot delete and then you pass in oops you pass in the confirmation so here you can say just delete that particular confirmation but that's up to you depends on how you want to do that I don't need to delete the confirmation but the logic in this case doesn't really matter I just want to have something that I can work with so that we can send emails and then we're going to return true okay also you can check to see if the user is null like this can return all if we didn't find a new user so you can put more checks in here just to make sure that your logic is very robust but again it doesn't really matter for us we're just doing this so that we can put some context around sending an email but this is going to be the core of the course okay so that part right here where we're going to be sending the email so we got our user we enable them we save them in the database and then we return true so now what we're going to do do is we're going to work on some endpoints because we need to expose some endpoints so that we can allow requests to come in and then we can call these methods and then try to save the user and things like that so let's go ahead and work on that right now I'm gonna go ahead and close this off and create another package here and I'm just going to name it resource I've have a lot of questions for this why I'm naming this resource and that controller again the answer is it doesn't really matter you can name this whatever you like I just know that for rest API they like to name this resource so I just like to name mine resource but you can name it controller or rest controller or user rest controller it doesn't really matter so I'm gonna name this user resource and it's going to be the class and we want to add some annotation here so the first one is going to be the rest controller so we're gonna say rest controller and then I'm gonna pass in at request mapping so we're gonna see request mapping I'm gonna give it uh we can do something like slash API slash users and then I'm gonna pass in the required R Constructor because we're going to be doing a dependency injection again okay so in here we need the private final uh user service I'm gonna name it user service so we have the user service so we can call the save user in the verify token inside of this controller slash resource so I'm gonna have a post mapping so we're gonna see post mapping and this is going to be public it's gonna be returning a response entity of type unknown for now so I'm gonna put a question mark and then we're gonna call it create user or create a new account or something like that or register a new user and then we're going to pass in the request body it's gonna be the user and then we're just gonna save the user so another thing that I want to do is to return a particular response which is something that I like to do obviously you can like return a simple string if you would like or you can return whatever you prefer right but in my case I'm gonna go ahead and return a particular object if you've seen any of my tutorials or my courses you've probably seen this before but let's quickly take care of this because I like to have it so let's right click here go to class and then I'm gonna name it HTTP response and then let's do right quick uh protected um strain time step we're gonna do another protected for the status code and then let's do another one for the HTTP status from spring so we're gonna name this status and then protect it and a little string for a message and then we can do another one for I like to do a developer message so we can undo developer message I know I learned this somewhere I just can't remember where and I just kind of like it and then we can now have another string for the path like the path that the request was initiated on and then we can even have something like oops protected another string requests method like if you want to send that back as well and then the most important one is a map of anything which I'm gonna call data and we need some placeholders in here so question mark any question oops and then let's go ahead and import this map from util and then in here I'm going to pass in at data also the super builder in this case the super Builder is really important and another cool one is Json include so unless something is not their default value we're not going to include it so we're gonna say none or not and all if you prefer I just like it on default so this is why we're gonna return so we can copy this and then we can pass it in here instead of a string this is the guy and let's go ahead and import that and okay so obviously this method is going to be pretty simple if this was like a real application where we wanted to actually create a real user and stuff like that you would have a lot of validation on this method like to make sure that you're receiving good data from the user but that's not the case in our situation so we're just gonna get a new user here and then um set them equal to the user service that save user and then pass in that user so it's pretty simple and after that we're just gonna go ahead and return a response entity so organized response entity that created because that's a tool one and then we call the URI that create because that takes a URI like the location of the Noli created resource or user in this case and then we can pass in the body so we're going to say badi and put the semicolon here so in the body we're gonna pass in our response so we're going to say HTTP response that build uh Builder and then we're gonna pass in um the timestamp I'm gonna call now our local the time so local date time that now that do string and then I'm going to scroll up a little more and after that we can pass in the data in this case that's going to be a map of the user so here we're going to pass in a string a user which is going to be the user while the new user in this case so no user and then we can pass in a message we can say user created and then let's pass in the status we're going to pass in created and then we can pass in lastly the status code status code uh created that value well lowercase value that's going to give us the actual status code like the number okay and then lastly we're going to call all build that's so that's gonna be our method let's import this map from util and I'm gonna do Ctrl alt L to format this a little well that did nothing but in any case so this is going to be the method to create a new user like I mentioned you would probably have like a user dto request instead of the actual user in your domain that you're saving in your database but that's something you can do we're gonna save the user which is where the sending of the email is gonna happen and then we're gonna return this nice response in the response pad okay so the next thing we need to do is to create just the second method to verify the token so let's go ahead and work on that right now I'm gonna go ahead and copy this first method and then go down paste it this is going to be a get mapping so we're gonna change this to get mapping and this is gonna be uh let's name it confirm user account or new user account whatever you want to call it and then we're going to pass in the request parameter so we're gonna say requests program and this is going to be the token so we're gonna be looking for the token as a request parameter and we're gonna translate this into a string or string is going to do the translation for us and then that's going to be the token so we have access to the Token we're just gonna call the verify token we named it yep verify token we pass in the token and it's gonna return a Boolean so I'm gonna get rid of that and we can do something like that too far and we're gonna say uh is success or something as the name of the variable and then we're gonna pass it in here so we can pass in the key here as uh oops Ctrl Z as success and then we can pass in this Boolean is success like that and then we can change that to account verified passing on OK here and then passing the study says the 200 we're gonna name this to the okay and then get rid of this CRI because we don't need it and then import this get mapping import so that should be everything we need to do for the confirmation of the of the user account by passing in the token so now we have everything we need to give this some some sense right so we have users coming in they're signing up and they're gonna pass in their email and their name we're gonna call the save user and if I do Control Alt B I'll go to the implementation for this and then we're gonna check to see if the user already exists if the user already exists meaning the email is already in our database we're gonna throw an exception and otherwise we're gonna sit there enable false and then we're gonna save the user in a database once the user is saved in a database we're gonna go ahead and create a new confirmation for them and then save that confirmation with the user information and then send the email and then return that particular use in the case that they send the token like after they open the email and click on the link that we're going to send them then we're gonna get the token as a request parameter we're gonna verify that token which will end up over here and all we're gonna do is try to find a confirmation by this token get the user buy that confirmation set their enable to true and then save that user in the database so that way the user is now enabled let's say we were using this enable to check whether or not they can access our app if this is false then they can't if this is true then they can so that's when we would make that change we save the user back in the database because we just made the change to the user and then we return true all other logic is really up to you but that's all I'm gonna have for this because I'm only using this to give some context around sending the email to the user okay and you can take this application as well and then you can build up on it and you will have all the code to create a new user and verify their token and you and whatever you want to add here and as far as logic is concerned so what we're going to do next is to just put in some database configuration because if we try to run the application right now it's gonna fail because we have the uh well we have to go to the pump file so we have this SQL dependency so it's going to try to find a data source bin and we don't have any data source bin so we're gonna have to make sure that we have the configuration for that so that the application doesn't fail and then we're going to run the app and make sure that our domains are being created in the database so the user and the confirmation and then we can hit Postman and then see if we can send a request to hit one of these endpoints just to make sure that the application is working as expected and then we're going to start working on sending this email okay so we're going to send multiple different types of email like I showed you in the introduction we're going to start with a simple text email and then we're going to gradually increase the complexity of the emails that we're sending so let's go ahead and put in a data source configuration and then we're gonna run the app to make sure that it's working as expected I'm gonna go ahead again and close everything and we're gonna go back to the configuration so inside of this resource I'm gonna rename this to a yaml file so change that property to yml and I'm just gonna paste in the configuration so I'm gonna paste it and we're gonna look through it so you can see that I'm setting in a spring profile obviously we don't need a spring profile but this is the code that I have sitting around and all the projects that I'm working on so just to give you a sense of what a good configuration would look like and I don't think that I've ever shown this before but I'm gonna show you now and that's one way that you can make spring configuration very decoupled unless you're using a microservice architecture then you have an entire application which is a configuration application but for our purpose we're going to be using a very decoupled configuration for this app it's overkill for what we're trying to do but you know it's better you know it than you don't so you can see that I'm setting a profile now a cool thing you can do when you're using a separate configuration file in your are bringing in these values or these variables is that you can set default as you can see here I'm passing this colon div so by default if spring is going to read this and it can't find it or it's null or something then it's going to use the dev as the default and you can do that with all of this verbs and then I'm defining the data source using postgre jdbc so relational database and then pass in the host the port and also the database name passing the username and the password and then all this other stuff is for jpa so generate ddl show the SQL logs we're going to update our tables every time we run and then passing in some time zone stuff and the dialect and format the SQL so pretty standard stuff if you've been working with spring boot that's nothing new you should be familiar with this configuration if you were to deploy this application in AWS you can set these as environment variables so you define this as environment variables and spring would pick them up but what we're going to do is to Define different types of environment within our local configuration so I'm going to right click on the resource and then do a new file and I'm going to call this application Dash Dev diamo so this is going to be the dev profile that's why I'm naming it Dash Dev this is also a convention in spring so I have application Dash Dev so that means that's the dev configuration and then I'm going to press enter and then I'm gonna pass in these values okay so it is for the database and this is for my server I don't think I'm using the server Port but we're gonna plug it in in a minute so I have the postgre as my username and the password is let me in super secure password and this is the computer where my instance of postgre is running and then the port and then SP cloud is the name of my database I don't know why I named this SP Cloud I don't even know what it stands for I'm pretty sure I had a good reason when I did it but I don't remember why I named this database SP Cloud but in any case and then since we're inside of this Dev application file then we set the active profile to be the dev okay so we're matching the name of the file with the active profile as part of the verbal that we're passing in to the main configuration file okay so it will replace this active profile right here which is already having the default as so the cool thing with this is that you can also let's say I can just copy this and paste it and then I can say for example tests and then in here I can just change these two tests and then I can pass in the test configuration values for these and if you want you can paste it one more time so oops I'm gonna click on this resource and then paste so here I can see something like Pro so if I had like a prod environment then I would change this to and I'll pass this file obviously I mean you wouldn't pass these values in the clear like this but there could be other configuration or there would be other configurations that you would pass in in prod your database information is passed in at runtime is if your application is being deployed in AWS you can use secret manager or whatever the case then but you probably not gonna pass your credentials or your for your database to the application line but you can see the benefits of having this type of configuration another thing you can do is you can go inside of this configuration so the one that we're using to run the application inside IntelliJ and you can set these um these values as well so you can see active profile I can pass in Dev or I can pass in tests and it will pick the appropriate configuration file and as you can see here so if I set this to test for example click apply click ok now when I run the app it will pick up this Dev this is just for running it in IntelliJ now if I would also do it in the terminal um I'm gonna pick git bash instead because I'm on Windows I don't want to use the Windows command line I could say nvn spring boot run whatever the I think it's spring Dash boot and then run and then I could also pass the active profile and the way you would do this is by passing in Dash D I think it's uh spring oops you do spring that profiles that active equal and then you can do prod whatever the case so or you can do Dev so it's very flexible in terms of which configuration that you want to be picked up whenever the application is starting so really good for development when you have to develop your application you know local and you can only use a Dev environment or test environment so very very flexible in real world that's how configurations are defined all these values are being passed in as environment variable and you can also even Define these in your server so on the computer where the application is going to be running you can Define these as environment variable on the operating system so really really flexible stuff that you can do when you have your configuration defined like this all right so I think that's everything we need so obviously we just have the same information in these but it would be up to you to change this information so I'm going to close the prod and everything else and if we go inside of the dev this is the configuration that I need and by default I'm going to be using the dev so I don't even have to do this right here I can just delete it click apply and then click ok so when I run the app well this is defined inside of the dev so it's going to pick this file anyway and the last thing I need to do is to use this port so let's go into the main configuration and then go to the bottom here and then we're gonna do server port and then we're going to pass in this value so we're gonna do the sign open curly braces server underscore Port you can see that spring can see these values because it has support for it also another thing that would be different is the port the port would probably be different for prod let's say prod would be 8080 let's say Brad could be like 80 80 or something like because you would probably have a different port for a different environment and then maybe that would be like 7000 and then all these would be different but anyways you get the all right so for Dev um we're gonna be running on 9000 and I'm gonna close this so that's gonna be picked up here I'm gonna put a space otherwise we can just stick with the old 8080 but since this is going to be resolved to a value then it's going to use the value instead of 80. okay so hopefully this was a good configuration for you and what we're gonna do next is to just run the application so let's go ahead and do that so let's click on this green play button and let's just give this application a second to come up here so we should see some logs that is creating the table okay there there we go so you can see everything was really fast so let's scroll up a little so it creates the confirmation and then it creates the users table so we have users and in confirmations and then it altered the confirmation table and add the constraint okay so you can see here at this unique construct of the user ID on the confirmation table and before we did that it checked to see if the constraint already exists so you can see here it's going to use this name for The Unique key and it checks to see if there is already an existing constraint by that name and then you can see where it adds the foreign key constraint in this slot and then the application started on 9000 which is the port that we're passing in right here okay so everything seems to be working so let's go ahead and check the database and see what we have so let's open my database and this is the PG admin so the reason you see that I'm using an IP address instead of localhost for all these things is because I have a computer that's like sitting in my room right over there and then I have Docker running on it it's got Ubuntu to running and then I have Docker installed so whenever I need like an instance of MySQL or postgresql or the PG admin I just create a quick Docker file and then I just spin up one of these instances of servers and I just use it so let's right click and just refresh and we can see the user stable so we can go to properties and we can look at the columns and you can see user ID email is enabled the name and the password so that looks all good and then we can also look at the confirmation go to properties go to columns and you can see the ID create a date which is a timestamp the token and then the user ID and if we go to constraint we should be able to see the confirmation key which is referring to the well that's the primary key but we want to go to the foreign key here that's the name of it and then you can see that it's pointing to the ID on the users okay you can see that in the reference here so everything seems to be in shape so the last thing that I want to do is to try to register a new user and I'm going to use Postman to do that so I'm gonna go ahead and open Postman and open a new request we want to go to well localhost well I've been testing so Postman is keeping a history of what I've been doing so we want to go to localhost 9000 and then we want to go to users and this is going to be a post request go to the request body and we want to select raw Json and then we're gonna pass in the Json in here so we're gonna need the name oops so name of the user so that's going to be junior and then we're gonna pass in the email of the user so I'm gonna copy this one two three I think we can pass in the email and then we're gonna pass in the password so password uh well the password is not being hashed that's another thing but if you guys know how to do this using Spring Security and another thing I want to show you even if we pass the enable so for example we're gonna say enable and then we set this to true we know that before we save we're gonna set this to false so even if we were to pass this enable it would still save the user as enable being false but I'm Gonna Keep it here so that you can see that even though we're passing in true here because we're updating it in the code then it's going to be false and then here I'm just gonna pass in Junior at gmail.com this is not a real email we're not really sending an email at this point so it doesn't really matter and oops I need to get rid of this okay and it's a post request so if we send this request we should get a success response so let's go ahead and send it and you can see or 201 created user is created and here's the new user and you can see the enable is false even though we're passing it through here because we updated in a code so now if we go to the database and go to the user and we want to do refresh and then I'm going to click on the tables and then do query tool so that we can open the query tool and I'm going to do select star from the users table and also we're going to do the same for the confirmation table so let's copy this and pass in the confirmation so we're going to say confirmations so if we select everything in the user we should see that one user in there we can see the user is in there and is enable is false so now if I select everything in confirmation you can see the ID for this particular entry the timestamp the random uuid and then the user ID okay so you can see the user ID here 4752 the reason this number is not one and it's also the same number it's because I've been using this database for the entire testing that I was doing so that's why it's not one and every time we create a new user we also create a new confirmation so these numbers are consistent so you can see the user ID is also 475. but in your case it's probably going to like one or two or something so just keep that in mind okay so at this point everything should come together so now you understand what we're trying to do and now we're just gonna go ahead and see if we can send an email to the user once they create a new account with us so if we go to the service implementation go to the user service so right here we need to send the confirmation with the token okay so this is a good time to take a break and if you want to use this application feel free to use it obviously everything we just did we didn't have to do it but like I mentioned we're gonna give this some context just to you know brush up on your skills a little so that you can understand what we're trying to do so now we're going to be focusing on this part right here which is to send the email to the user with the URL and the token in the URL and I'm gonna send you how you can send this email in many different ways so this is a good time to take a break if you would like and then come back but that's what we're going to be working on right now the email the sending of the email so let's work on that right now I'm gonna be using a Gmail account so that I can send the emails but you should be able to do it with any mail server that you have so if you have Outlook or Microsoft or whatever other mail server that you're using you just have to find the credentials like the information that you want to be able to login into that server that spring boot is going to use and then you should be able to achieve the same thing but I'm going to give a Gmail example because you know most people have a Gmail account but you should be able to follow along as long as you can find the information that is needed for springboot to be able to log into your mail server and then send the email so let me show you the steps to do for Gmail so if I go back here and I have this page open and I'm going to zoom in a little more so what you're going to need to do you have to create something called an app password and these are the steps that you have to follow so that you can do this so if you go to your Google account you go under security and you select two-step verification I believe you need to have two-step verification set up in order to create a an app password so make sure that you have to step verification setup and then you should be able to generate an app password as you can see here so these are very simple steps and maybe most of you already have that but if you use your password that you use to log into your account like on a daily basis then it's not gonna work you need to have an app password for this to work okay so make sure you follow these steps and then create the app password that we're going to be using in our configuration in Spring boot so that springboot can access the Gmail server and then send the email on your behalf and if you want to see what that looks like I have my page open obviously I'm going to be blurring things here and there but I just want to show you what that page would look like so if I go to my Google account that's what it looks like and as you can see right now I'm under security so I'm right here in step two and then I have two-step verification set up so you can see the green tick so if I click inside of two-step verification it's going to take me to this page and again I'm seeing personal information on this page so things are going to be blurred out and I do apologize for that so if we go there and check to see what we have to do so it says at the bottom of the page we should see the app password so if we go back here and then I'm going to scroll to the bottom so here I am at the bottom and you can see I have one password okay so you just click here and then it's gonna give you this link to generate a new password so you would just click that link after you click here so if I click here it's gonna take me to this page you can see that I have one already and then you just have to select the device so here it's not a mail calendar or YouTube or contact you just have to say other and you can see like my app or testing or something and then you're gonna click generate and then that would generate a random password so if I do that you can see it gives me this password that I then can use in the spring boot application to log into my Gmail account and I'll click on done and then you can see my app is created here and I haven't used it but I don't need the second one I'm using my web app so I'm going to delete this one so it's deleted I'm only using this one my web so make sure you follow these steps so that you can generate the password and then you should be good to go so I'm going to go ahead and just close this out and then now what we're gonna do is we're gonna go to our configuration so we're gonna go to the dev configuration and then we're gonna put the configuration all the values that we need to pass in for the email server and then we're going to plug these values in inside of the main application property file which is the application.em so let's go ahead and do that right now so I'm gonna get down here and let me close this and what we want to do is well I'm going to add a let's say email config and under there I'm going to create some custom properties which is what we've been doing so here I'm gonna say email underscore host and then we're going to set this equal to the Gmail server so smtp.gmail.com and then we want the email and then we want the port and I think for Gmail it's 587 and then we're gonna do email underscore ID so that's the email address like your actual email address like john123 gmail.com so this is going to be um let's do your email address you can name it email address in this case I'm just naming it email ID but it doesn't really matter and then here I'm gonna have the email underscore password so that's going to be the your app password okay so whatever we just talked about and then the last one I'm going to call it verify underscore email underscore host since I already have an email oops I would type it here so since I already have a email host here I'm just gonna name this the verify email host and I need this to be http colon W4 slash localhost and then we're gonna pass in the port so here we can see Dallas line and then open we're gonna pass in the server Port so server port in our case that would be the 9000 that we have here so whatever port for whatever environment that we're running on that's going to be the port of this host so obviously this would be your domain name like your core domain so you would pass it here or you can just pass this in at runtime depending on the environment or if the application is being deployed so it can really be whatever you want it to be so now that we have this configuration you can see they have this underline like this squiggly underline that's because they're not being used so we're gonna go ahead and use these values and also don't forget to actually pass in your credentials here so that would be your Gmail account or your Gmail email address and then that would be the app password not the password that you use to log in if you use that password then it's not gonna work it's gonna say it couldn't log into your account it's going to give you some kind of an error so make sure you use the app password that we just generated or we just talked so your email like the the same email address that you use and then your app password and everything else should be the same and also I can just copy this configuration and then I can make sure that I have it in all my other environments so I can go here and then paste it and then you know if I have to change something here to match the product environment then I would make that change here but just so you know that it's gonna go in all of the different environment configuration files okay so that's going to be in here and also remember to update everything appropriate okay so we can close these now and now we're gonna go to the main application file and then we're going to pass in these values that we just created so the configuration for the email they go under spring so I'm going to collapse some of these stuff here so that you guys can see better so we're gonna go under spring so right here you know spacing is very important in a yaml file and then I'm going to pass in mail so in the mail I want the host and this is going to be our host so here we're gonna pass in the email host and then we're gonna pass in the port so as a port and that's going to be our Port so email Port you can see it coming up here and then we need the username so oops username that's going to be the ID so the email ID so that's going to be email underscore ID so that's your email or your username and then we need the password so we're gonna see password and that's going to be the password and grab this here and I'm gonna pass in a few other things that are quite important so I'm gonna pass in the the fault encoding and then I'm gonna set this to UTF eight so that's the encoding for the email and then I'm gonna pass in some other properties so properties I'm gonna pass in a bunch of properties so we want the mail um and then we want to do mime and then I'm gonna do Char set again I'm gonna set this to UTF and then under mime I'm gonna pass in SM TP SMTP and then I'm gonna pass in right time out I'm gonna set this to 10 seconds so I'm gonna do ten into three so milliseconds and then I want the connection time out you can set this to whatever you prefer but I'm gonna be sending the email and a separate thread so I'll give it 10 seconds Max try to establish a connection and then I'm gonna say time out also ten one two three and this is my experience because sometimes when I set it to five I see that it fails and then I decided to up it to 10 but that's gonna be up to you and then the auth we're gonna set this to true and then we want the start TLS because we want to transport layer security we're gonna set this to well we need to enable it so we're gonna say enable said this to true and then we want to see required because we want a secure connection so we're passing in some properties here so character set for the mime type is UTF SMTP so that's the protocol we're setting some properties for it so the simple meal transfer protocol that this is going to be using to send the emails we're passing in the right timeout to be 10 seconds the connection timeout to be 10 seconds and the overall timeout to be 10 seconds as well you can look at these properties online and see exactly what they do but most of them are self-explanatory so they should be easy to understand and then we're enabling auth and then we want the transparent layer security to be enabled and also it's required and then under property I'm gonna pass in the verify so I'm going to say verify this is a custom property and then I'm going to say host and then I'm gonna pass in the host so we're gonna say verify email host okay so this is a custom one that is not part of these properties here but you can add whatever you like and just use it in your application with no problem so you can put it in any bean and it should be picked up and I'm going to show you how we're going to use these okay so that should be everything that we need for the email configuration you can also do this using a Java class which is pretty simple but this is at least in my opinion a better way to do it well in the Java class you can also pass in properties like we have here but I prefer to do it in a properties file but it's really up to you but you can do the same thing in the Java class you just create a simple configuration class and you can use the I think it's called the Java mail implementation or something like that and you can pass in the same configuration like the host the port the ID the password Etc so that's all the configuration that we're going to need for the email now we're gonna go ahead and make use of them and then connect to the Google email server and then see if we can send an email I'm gonna go ahead and close everything here and we're gonna go back to the service so inside of the service folder I'm going to create another service so this is going to be the email service so email service and well this is a as I want it to be an interface so we're going to change this to interface okay so inside of this email service I'm going to Define all the different methods that I need to send email so they're gonna return void and I'm gonna say send simple mail message and this is gonna take a string uh the name like the name of the first name of the user another string that's going to be the two like who we're sending it to and then we want the token as well so we're gonna say Tok so the token is going to be the token that we're gonna send with the email so that the user can click on and then enable or confirm their new account and then I'm gonna copy this and then paste it down five times so one two three four five and then I'm gonna change the name so I'm gonna change this to send mime message with attachment okay so I'm gonna finish I'm gonna show you how to send message with attachment and then we're gonna change this to maybe send my message with embedded uh so we're gonna use the my message helper so that we can send them my message and then we pass in some embedded file or something like that I mean you can come up with a better name for this and then this next one is going to be almost the same so let's copy this and then paste it here and let's pass in files uh I can change this to embedded images because a lot of times when you pass in embedded images they work just fine because they're like inside of the email body but if you pass in some files depending on the file type it might be hard for your email client to display it properly but if it's on image then they can display it properly so that's why I'm having one to send my message with embedded images and I have another one to send my message with embedded files so you'll see the difference with the two and then this one is going to be send HTML email so that's what we're going to send a simple HTML email and then the last one is going to be send HTML emails with embedded files okay because inside of the HTML emails themselves you can put pictures or other files and things like that so I'm going to show you how to do that as well so I'm just trying to cover as many scenarios as possible and then I'm going to show you the same but this time not with images but with files and then I'm going to show you how to send HTML emails like these rich emails that you get from your retailers stores or something like that where they have like images like it looks like a web page so that's an HTML email it's really just an email but in an HTML format and I'm going to show you how to put files or attachments or embedded files inside of these HTML emails as well so we're just going to try to cover as many scenarios as possible so now we're going to give an implementation to this interface and then we're going to start working on each one of these methods so let's go ahead and do that right now let's go over to the implementation folder so this folder right here and we're gonna do email service MPL for implementation and then we want to implement the email service and then we're going to give an implementation for all of these methods so select OK alright so the first thing that we're gonna need is the mail sender so I'm going to say private final Java male Cinder and then I'm gonna name it email sender so this is the guy that's gonna send the email on our behalf and then here I'm gonna pass in the service annotation and also passing the required RX Constructor so that we can do the dependency injection in the Constructor a couple other things that we're gonna need we need the um we need the host so we're gonna say private string string host and also we need the from email like the email ID that we're using so private string and it's going to be from email and this is supposed to be string and I'm going to use the add value annotation to plug those in whenever this bin is created so I'm gonna do quotes and then dollar sign open and we want to go to Spring dot mail dot verify that host so that's something that we gave the host and then I'm gonna copy this and paste it on the from email this time we need the username so I'm going to change this to user name so the username is like the email ID like your email address in this case that's going to be the from email that we're going to be using here so at this point we have almost everything that we need inside of this class so that we can start implementing all of these methods obviously we don't need all of them but every time we're gonna use one and then we'll just start a new user and then you're gonna see what the email looks like so you can take this app and then just take the entire code you can put it on your whatever application you have whatever the complexity and then make small modification to it and then just use one of these depending on your use case so if you want something with embedded files or HTML email whatever the case then you can just grab one of those and use one of them or use all of them it's up to you depending on your application so now what we're gonna do is to try to send the simple mail message so this is going to be pretty simple so let's do that right now so I'm gonna do try and I'm gonna do a try catch black actually so try and then we're gonna do catch any exception so I'm gonna do exception exception and then if we have any exception we can just do a CS out and then log it so let's go down here and then put this on a new line so I'm gonna do a csap and then just do exception that get message because we're not really doing any proper exception handling in this case and then we can just throw an exception so throw new runtime exception and then we can pass in the exception that get message so for our exception we're not really doing anything we're just printing it on the console and then throw the exception which is gonna also print on the console but it doesn't hurt so now we're gonna pass in the message so we're gonna say simple it's supposed to be uppercase so simple mail message that's going to be the message and then we're going to set it equal to a new simple mail message and then we're gonna say this message that sets subject so the subject of the email we're gonna set this to maybe something like new user account verification something like that so let's make uh let's make a constant out of that so that we can use it in other places so introduce constant no user account verification that looks fine to me and then we want to set the from so message that is set from that's going to be the from email the one that we're grabbing from the properties file and then we're gonna pass in the message that two so set two so who we want to send the message to that's gonna be the two so whatever email address that we get here and then we're going to set the actual email itself so the message of the email so that's set text so this takes a simple text message um so we can say something like hey this is working so that's just for tests and then after that we're gonna pass in this token and send everything properly we're just gonna do a quick test and then lastly we want to send this so that's what we call the email sender that's send and then we're going to send that message so pretty standard stuff very simple well the simple mail message is supposed to be simple so you can see it's very straightforward it's very good when you want to send a quick text email but if you want more options you might want to use something like the my message and the my message Helper and I'm going to show you these as well so this is going to be the first example we're just going to send a simple mail message so what we're going to be doing is to test this out and make sure that we can at least send an initial email just to make sure that things are working so don't forget to update your application Dash dev.aml file I'm not going to open it right now because I have my credentials in there but remember to change this and then add your password and email in there otherwise obviously you will get a failure and then I'm gonna go inside of the user service implementation and then I'm gonna scroll up a little more and then I'm gonna pass in the private final email service I'm gonna name it email service now we can use that email service and call that one method that we have the implementation for which is just a simple mail message so let's go down so after we save the confirmation we can pass this information in so we're going to call the email service that sends simple email to that one at the bottom here since this is the only one we have an implementation for and then we're going to pass in the name of the user so we're going to see user.getname we need to pass in the two which is the email address or the two email so that's the user that get email and then the token the token is under confirmation that get token it's pretty simple we're not using this token right now we're not using the name of the user but that shouldn't hurt anything so if we start right now we should be able to send an email to this user's email which means that we're gonna need to pass in a real email to that user whenever we're creating a new account or supposedly creating a new account since we're just doing this for testing so let's go ahead and take that for swirl so I'm gonna go ahead and run the application first so let's click on this green play button and just give it a second to start uh it's going to come up here in just a second okay there we go so the first thing is we want to make sure that everything starts properly like we don't have any errors or anything okay so you can see it started on 9000 so everything seems to be working properly so now I'm gonna go ahead and open Postman and then we want to go to the request body and this is what I was using this is not a real email so I'm gonna go and pass in the yeah arrays so this is the email that you can use to send me an email if you have questions so that's a real email so make sure you put a real email in there otherwise you won't get an email so you need a real email at this point so I'm gonna go ahead and send this request now and let's see what happens so let's give it a second so this is going to take a little longer because it's gonna take more time to send the email and we're gonna fix this in just a second but initially I just want to test it so that you can see what's going on so you see that it's taking some time well it's taking too much time and then finally we get the 200 okay that the user was created so we got our email our enable is false and then we get a 201 so the user was created so now we can check the database to make sure that the user is actually inside of the database and also check our email to make sure that we did receive the email so I'm going to go back to my Gmail and again if you see things are blurred that's because I have students sending me questions and things like that so I'm just guarding their privacy and you can see the first email that I have is new user account verification and you say hey this is working hahaha so we can already see that this is working and I'm going to go ahead and click open it and you can see here's our email which is just a simple text hey this is working hahaha all right so we know that this is working so now what we're going to do is to make sure that we can send the link to the user so that they can click it with their token and then verify their new account so let's just delete this message and then go back to the application well we need to go back to the app and I'm going to minimize this and then go back to the email service implementation so we need to make use of this token and then generate the URL that we need to send with that email so that the user can just click on it and then they can verify there so let's go ahead and do that right now I'm going to go ahead and create another package so let's do util so new package and I'm going to name it utils and then I'm gonna create a email util you can name it message utils or something like that so email utils and here I'm gonna have two methods so the first one is going to be public and static it's going to return a string and I'm going to call it get email message because I don't want to do all this string manipulation inside of the email service implementation so the first string is going to be the name the second string is going to be the host and the third one is going to be the token so we're gonna get the token the host and the name and then we're gonna see if we can create that email message so that's going to return a string so we're gonna say hello let me close this for more space so hello and then we're gonna pass in the name we can use a string Builder to do this which is going to be a little bit more performant but this is just one line so it shouldn't really hurt anything so we're gonna say hello and then pass in the name of the user so we're gonna see you know hello and then we're gonna pass in two new lines so I'm gonna do backslash n twice then I'm gonna say your new account has been created please please click the link below to verify your account and then I'm gonna pass in again two more new lines so backslash n backslash n and we need to pass in this URL now so let's create another get verification uh URL so that's gonna be another method we're gonna give it the host and we're gonna give it the token and then close this off with a semicolon so now let's create this method so create method also going to return a string so all we're gonna do is concatenate the host so we're gonna say return the host we're gonna add in the um we have to go to slash API slash users and question mark token equal and I'm gonna pass in the token so we're gonna say plus the token just like that so that's going to give us the URL that we have to send to the user so that they can click on it and then confirm their new account and then uh if we want we can see something like we can put some more uh new lines we're going to say two new lines um well we need backslash n and backslash n and then we're gonna say the support team okay and we can put that on a new line I'm gonna do Ctrl alt L okay so it didn't reformat that so I'm gonna just drop this on in the line okay so the get email message is going to be a low whatever the name of the user is um we can probably put a comma so after the name we're gonna put a comma and then do two new lines and then we're gonna say your new account has been created please click the link below to verify your account we put two new blink line we put the URL this is going to give us the URL and then we put two new blink lines and then we say the support team so now we can use this email util and then call this method pass this information and then it should give us the string that we need for the message so inside of the user email service implementation well we need to go here instead of this text that we're passing in here we're gonna say get our email uh email util and then we're gonna say get message and then we need to pass in the name the host and the token okay so that's going to give us the message that we need to pass in as the text message and then let me just import this statically and do Control Alt oh all right so we should be good to go now we should be able to see the URL that the user needs to click on and then they should be able to verify their account once they click on that URL so let's go ahead and take that for a test so I'm gonna rerun the application and I'm gonna go back to the database and I'm going to select all the users let me send s here select so we should see the two users that we have used so far and we should have two confirmations for them as well you can see it here so I'm just gonna go ahead and delete this information so I'm gonna say late from users true and I'm going to delete all the users I'm gonna do the same for the confirmation so if we try to delete the user State uh the information the users table is going to give us an error because of the foreign key reference so we need to delete from the confirmation first and then delete the users okay so now we shouldn't have any users you can see no users also no confirmation because we delete okay so let's go back to the application so we're up and running so that looks good we're gonna try Postman one more time so let's open up Postman and we're gonna send this email one more time just so that we can see what the new message looks like so let's just give this a second I'm gonna fix this right after this one test because this is taking too long I'm just gonna make sure that the sending of the email is done in the new three okay so it looks like we were good so 201 created and the email should have been sent so if we go back to the app we should have any errors okay so everything looks good let's check the database so let's open that up real quick and select all the users that one user is entered and their confirmation should be in there as well so if we select you can see their confirmation is in there and you can see I have one email so let's go in there and let's look at that email so you can see this email is much better we have hello Junior you know kind of has been created I'm gonna zoom in a little more and then it says click on the link to verify your account here's the link and then the support team so that looks perfect right so if I click on this link now you you can see I can't verify so we were able to verify our account by just clicking on the link that we got from this email so this is one way that you can send emails simple text message email or text emails and a lot of times this is just all you need because you can see this is you can make this look more professional you know by just changing the verbiage of the message or something like that but that should be everything you need I mean there's like Government website that when you're using them and you need to confirm your email or something like that they send emails that looks exactly like this so this is a lot of times enough for a lot of application because you just need to click on that link and then you're gonna just do whatever you need to do with this token and then they can just confirm their account or whatever the case might and you can see that this token that we have here should be the same that we have in a database so it should be this took right so that's why we were able to get this success response and then verify the account of the user otherwise it would give us an error okay so you can see that this is working this looks really nice and that's all we're gonna have to do for the simple mail message because we're just sending simple text in the email so this is working just fine and I'm gonna go ahead and close this and also delete this email and then go back to the application okay so the first one is completed obviously you can do more error handlings and things like that but that should be all we have to do and now I want to make sure that this runs in a separate thread every single time so let's go through the main application file so this file right here and then I'm gonna enable a sec so we're gonna say enable oops enable a sec okay so we're gonna enable a sec in the entire application and then we're gonna go back to the email uh service implementation and then we're gonna add the add a sync annotation so we're going to say async so every time these methods will run in a separate thread so let's paste this down here and here we're just gonna do it for all of them so that we don't have to wait that long you notice when we create the new account it takes a lot of time for the response to get back to us but we don't want that to happen so now all of these methods they're going to run in a separate thread so let's rerun and then we're gonna do one last test so let's go back to the database and let's just delete everything again so delete all the confirmation delete all the user okay and I already deleted the email so let's go back make sure we're up and running okay so we're up and running back to postman you will see that it's significantly faster because once it gets to this method then it's going to dedicate a separate thread for it to run and then it's gonna return the response to us in the main thread or the thread that was created with this request so if you watch this you can see that I'm gonna click Send and you can see we get the response almost instantaneously okay so we don't have to wait for the whole sending of the email to be over and then get the response this is the typical Behavior you would have whenever you're sending emails anyway because you don't want to send it in the same thread and then have the user all right so now if we go back in the app you can see we have no errors everything works fine and we can check the the database so let's go back to the database select all the users our user is in there their confirmation should be in there as well and we see that we have our one email so if I open this again we get our email so that's all we're gonna do for the simple meal message so this method right here so this one is completed the next one we're going to be working on is the one that we have down here what we're gonna pass in attachment to this email so that's the one that we're going to be working on right now so let's go ahead and do that I'm gonna copy everything that I have here and then scroll down and let me just collapse these because we're not gonna be working on them right now and we're gonna paste everything in there so I'm gonna paste so this time instead of using a simple meal message we're gonna use a different type of message we're gonna use one called a mime message so you can see it coming up here so the my message on that my mail message just my message gonna import that okay so we have the my message so it's gonna be the message and I'm just gonna create another method so I'm gonna say get my message it's not going to take any parameters because I'm going to be using this my message multiple times so let's just create the method so this is going to be pretty simple all it's going to do it's gonna return the email sender that get or create my message okay so that's going to give us the my message that we need to work on with that email sender and then I'm going to cut this off and put it at the bottom right and collapse this I don't need to collapse the and I'm just gonna collapse the annotations just make sure that we're only focusing on what we work so with the mail sender we can create a my message and that's what we're doing here and we're gonna have this my message gonna go back up collapse these again and then scroll here okay and now we're gonna need the my message helper so here we're gonna say my message helper that one we're just gonna name it Helper and then set it equal to a new my message Helper and this one is gonna take the message so the my message and it takes also the multi-part because we're going to be passing in files so we're gonna pass in true so you can see multi-part true and also we can pass in the encoding so in this case we're gonna pass in utf-8 so we're gonna say utf-8 for our encoding and that's all we have to do for the message help I'm also gonna turn this into uh constant so into this constant so utf-8 underscore encoding just like that okay so utf-8 encoding we're gonna have multi-part which means we're gonna have files in this message and also defining our helper so that's how we Define our help now the helper has more options than just the simple meal message that we were using in this method so for example one thing that I can do is I can pass in the priority of the email so if I paste this in there and I can say set Oops said priority and I think it's from one to three or something so if it's one then it's very high priority and you can go in there as well and it's just gonna set this header for you so the X priority which is an email header and if you had that then the email is going to be marked as important or something and depending on the email client that you're using the any match show a little bit of a red text or make sure that it doesn't end up in your spam folder or something like that so you can go inside of this helper or just look at all of these Setter methods or other methods that you can use so you should be able to see all of them and then we're gonna pass this helper now for the set subject so the helper can also take the set subject also pass in the helper for the set from the set two and also the set text okay so now we're working mostly with the helper and they also share the same method so you saw that the message also has some of the same methods so if I go in there and scroll down a little more well it's a big class maybe that's not a good idea but if I do for example message and I do that you can see if I do set you can see set from so they have they share some of the same methods but we're going to be sticking with the Helper because that's what we're using so we set the priority we set the subject we set the from email we set the to email and then we set the message text itself so before we can send this actual message we can pass in the attachments that we want so here I'm gonna say add attach mint okay so we're gonna be adding the attachment from this point on and I have too many D's in here okay so here what we can do is we can do something called a file system resource so file system resource and make sure that this is coming from spring.core.io and this is going to be uh well let me show you the pictures that I have on my system so I'm gonna open the pictures that I'm going to be using so you can see I'm inside of my downloads and I have a new folder called images and then I have this first image so this is an image of a fort that is in my hometown in the country where I was born and this fort is on top of a mountain so as you can see here it's a beautiful Fort and the second feature that I have is a dog so look at this beautiful fella so this is the dog so we're gonna use the fort so the first picture is the Fort and also the dog so these two pictures and then also I have a document so you can see this homework that I have here I just grabbed some homework that I had sitting on my computer and then I put it in this folder so we're gonna be attaching these three files in the email as attachment so let's go back to the app so the first file system resource is gonna be the fourth so I'm gonna name it fourth instead of equal to a new file system resource and then in here we need to pass in the new file so we can see new file and then we're gonna pass in the path where the file is so we're gonna say system uh that get property and then we want to get the home page of this computer so use it at home and then after that we can go to the downloads folder so we can see plus and then we want to go to slash down loads and then remember inside of the images folder so let me bring that back up one more time so I go into my home directory and then inside of the downloads folder inside of the images folder I have the four.jpg dog.gpg and then homework that document or docx so we're gonna use these three files so I'm gonna say inside of the images we want to pass in the fort.jpg okay so that's going to be the first resource so that's gonna be the fourth and then I'm gonna do the same for the dog so the dog and also the homework so I'm gonna change this to dog so that's the dog I'm gonna rename the variable as well the dog and also change the homework so that's gonna be the homework and then change this to homework homework that the oocx whatever the file extension is you just put it there so homework that the ocx so that's the homework so now we just have these three files now we need to attach them to the email so the helper gives you a very very easy method that you can use to pass in this attachment so we're gonna grab that helper and then go back here oops so I'm gonna copy the helper and then we want to go on a new line and then I'm gonna say the helper that it attachment so you can see they have many different methods so one takes the file name and the data source another one just take the file so there are many different ways that you can pass in an attachment to this email so we're gonna say pass in the attachment and then for the file name I can just get that from the file itself so I'm going to say four that get file name and then I'm gonna pass in that four so we're gonna pass in the file and then I'm just gonna go ahead and just copy this paste it down two times one two and then we're gonna pass in the dog so pass in the dog and then the dog over here also passing the homework okay and we're getting a little squiggly on their line here that's because this might be null but we can just ignore it I mean obviously if you want to be super sure that this is not and all you can do a simple check if it's null then pass in something else but I'm not worried about it so we add the files inside of the email as attachment and then we're gonna send that email so that's all we need to do and we're pointing to all of our files so the homework the dog and the fort now I'm gonna just copy this name and go over to the user service implementation so we just need another line of this I'm just gonna paste this here copy and paste this get this name out and then paste it in here okay and I'm gonna comment this one out because I don't want to send two emails so we're going to use the send my message with attachment which is gonna call this implementation because it's the only implementation that we have for the server so at this point we should be good to go ahead and test this out so I'm gonna just rerun the application and then I'm gonna go back to the database and I'm gonna just delete everyone again so the delete this I mean obviously you don't need this to send the email but we're trying to put this into context so we have to deal with this user thing here we don't necessarily have to have this to send an email all right so let's go back and make sure we're up and running okay so we're up and running with no problems so let's go back to postman and then I'm gonna go ahead and send the requests so everything is successful so let's go back to the code okay so we don't have any errors or anything so now let's go back to the database and look for all the users and look for all the confirmation okay so you can see everything is in here now I'm gonna go over to my email and then I'm just gonna go ahead and refresh here and you can see here I have the new email and then I also have the three files you can already see them if you're using a Gmail account so I'm going to go ahead and click on that and as you can see we have our email so this is our message and also we have three attachments you can see the four the dog I can even click on it I mean it depends on the email client you're using but you can see we have the Ford here we also have the dog and also the file you can preview the fine but I think I have my name as you can see here at the top left corner of that file I have my name in my school name and my teacher's name in my school email so I'm not going to preview this file but you can already see it here so we sent three files with this email and this message at the bottom is because I've been deleting messages coming from this particular email that I'm using so so Gmail is telling me that I have deleted message in this conversation so you can just delete them forever okay so you can see this is our email everything looks good and you can test this more with different types of file all files should work and also this homework that I'm passing in here it's a very large file as you can see here it's seven megabytes it was a very large file I had to wait a little for the email to come through so if you're just testing this just make sure that you're using a smaller file just for testing But ultimately your email should come through and I'm not sure what the limit is because for emails I'm assuming you can transfer very very large files because you know that's something that happens all the time so I'm not sure you can just try to Google that and see the limit as far as like how many fars or what the megabyte is going to be and of course as a user you can click on the link and everything should work and this is a weird example we're setting a link to a user to confirm their account with pictures of dogs and force and homework but you know you get the idea so what we're going to do now is you can see that these are sent as attachments so you can see them here they're attached to this email as attachment and depending on the client that you're using you might not see this preview because this is something that Gmail is doing so if you're using an Outlook I think it's going to look a little bit different or maybe they're just going to be shown as attachment but what I want to do next is to make sure that I can pass in files in most cases images inside of the email bad itself so wherever we're seeing this text at the top here say that I wanted to embed this image inside of this email like this okay so instead of passing them as attachment you want to put them within the text of the email itself and there are many reasons why you might want to do this because say you just wanted that when they open the email they can see the pictures that you want them to see right and like I said this preview that we're seeing at the bottom is a Gmail brief if you have a different email server it might not show you this preview but what you can also do like I said is to embed the images inside of the content of the email itself so wherever we see the text of the email we can pass in images most of the times images but you can pass in all kinds of files so that's what we're gonna do we're gonna embed the image inside of the email body itself so I'm going to go ahead and delete this and then I'm gonna go back to the application and we're gonna scroll up a little more so this is going to be this one so send my message with embedded images so we're pretty much done with this one so that's going to be the attachment so what I'm going to do is to just copy this and then I'm gonna just collapse it because we're pretty much done with this one oops and then I'm gonna paste in here okay because it's gonna be very similar and I need an opening here and delete that okay so we're gonna be doing almost the same thing with this and I'm gonna do control all L just to format this a little and then also at the last closing okay so now we're gonna work on this method so that we can embed the images within the email bad itself and instead of passing them in as attachments as we're doing here so what we're going to do I'm going to scroll down a little everything else we have in this method is going to be the same the only change is going to be in the add attachment there's another one that we can call which is ADD and line so we're going to say add and line so the add in line it's going to take a Content ID so if I put my mouse over here it's going to need a Content ID and you see at the bottom here it says the content ID has to be enclosed with or less than sign or greater than sign okay so the ID is going to be embedded inside of these open and closing bracket so I'm going to go ahead and make the change in all the methods so I'm going to paste it in here so instead of add attachment we're going to say add line and then we're going to make sure that when we pass the name of the file we can put the opening and closing bracket so you can do something like this so you can say opening and then plus and then you do the closing so you do plus and then closing so that will work just fine but it looks a little bit messy so let's create another helper method at the bottom that's gonna just do this for us so I'm going to scroll down and just gonna copy this private and then I'm gonna go down and paste it down I'm gonna change the name to get content ID because this is the name that this thing has like the first parameter so if I put my mouse on here you can see it's content ID so I'm going to name this get content ID and then I'm gonna format it a little okay so now what I want to do is I want to take the file name so in here we're going to take a string that's going to be the file name so string file name and what we need to do is to just put the closing and the opening around the file name so I'm going to say open and then we're gonna pass in the file name so we're going to say file name and then plus and then open and I'm going to change this to a string okay so we're returning the string which is just the file name with the opening bracket and the closing bracket and that's all we have to do here and I'm going to remove the space at the bottom so now we can go and just call this method so instead of doing all this stuff here I'm gonna get the file name and then pass in the get content so get Content ID and then pass in the file then I'm just gonna copy this replace it here just like that and then pass in the appropriate file so change this to the homework and change this to the dog too many of these here okay so we're passing in the content ID which is pretty much going to be the file and then pass in the file so the one thing that I want to say is I don't know what's going to happen with the homework I do know that the pictures will be embedded in the email but I'm not sure how the mail client is going to try to display this very large file which is this homework file within the email itself as an inline attachment but we're just gonna go ahead and try it out so let's go back to the database and we're gonna just delete again all right so we should be able to register a new user let's go back to the application and we're gonna call this method so copy this and we're gonna go down call the email service so we're going to say email service that pass this name in and we're pretty much gonna pass the same information so I can just copy everything inside of this method just paste them in here and then what we're going to do is we're going to come about this one above because we don't want to send duplicate emails okay so that should look good and then we're gonna go ahead and restart rerun so now we should be good to go you can see we have no errors okay everything looks good let's go back to postman and I'm gonna go ahead and send this request one more time okay we got two one created let's go back to the app scroll down there's no errors let's go back to the database we should see the new user okay the user is here obviously their confirmation is also here but we can check anyway okay confirmation is here now I'm gonna go to my emails I got nothing yet and oh there it is so you can see that we have the new email and I'm just gonna go ahead and open it up so you see now Gmail tells me that I have three attachment and this one didn't have any name so it says no name but it looks like it's still the 7.1 megabyte but it wasn't rendered properly because you see that I can't see the preview and I bet if I click on it I won't be able to see anything okay so I can't see anything so obviously something was different here so what I'm gonna do is to just delete this and let's go back to the database well I'm gonna also delete it in my trash delete forever and then I'm gonna go back to my database and then I'm gonna delete I'm gonna use a different email address so it's delete this confirmation and then delete the user we delete the confirmation first because of the foreign key constraint otherwise it's going to give us an error so you have to delete the confirmation first and then delete the user or you can disable that altogether so now we've deleted this and now let's go back to postman this time I'm going to use another email which is an Outlook so I'm gonna just change this here I'm gonna blur this because this is my personal email so just so you know so I put in this new email which is an Outlook email and then I'm gonna go ahead and register this new user so I'm gonna click Send okay we got two uncreated now I'm gonna open this Outlook account and you can see the very first email at the very top is the email that I got for the new user verification and you see the little exclamation mark that is all in red that's because I set the priority to one so that's the highest priority for an email when we were looking at the email in Gmail we didn't see any indication that the email was a priority one which is like the highest priority but you can see that in Outlook we can actually see that because the email is a very high priority email then we have this red exclamation okay so now let's open this email I'm gonna click on it so you can see a difference you can see that these files are still added as attachment but they're actually in the email badge so I can click on one of them and I can preview it and I can even scroll over to the next one but for some reason you can see the homework file is not being displayed properly so I would say definitely avoid sending large files in the email body like this you'll send them as attachments but if there are images then obviously you can send them I wonder if I use a smaller file size if that would be the case but I'm not sure but in any case you can see now we have these two pictures they're kind of like inside of the email body even though it says attachment but they're like in the email body I guess they're still attachment but you know we can click on them in the email about you don't have to download them or anything like that and also it doesn't work for the file so what I'm gonna do is I'm gonna show you the difference in Outlook so I'm going to close out of this and then I'm gonna delete this so I'm gonna go ahead and delete this message and then I'm gonna go back to the application and then I'm gonna go to the user service and then I'm gonna comment this one out and then comment the one before so the one that is just pure attachment I'm gonna uncomment it and then I'm gonna rerun okay so we're gonna rerun then I'm gonna go back to the database and then delete and delete the use okay so I'm gonna send another request with the Outlook email again so let's make sure the application is up and running okay open been running so let's go back to postman and we're going to send that request again with the Outlook email and then let's go back everything looks good so let's go back to my email and I got the email so you can see that the email is different so looking at it before we open it before we click on it you can see that it doesn't show us the little image icons because when we had the inline like when we add them inside of the email body itself even in the preview you could see that they had a little preview of the file like the little file icon was showing but now you can see that it only showed a little attachment symbol so you can see there's a difference so now if I click on it it still kind of looked the same but now you see that the homework is properly displayed because they're like put inside of the email as real attachment so there's no problem because it was just adding as an attachment not embedded in the HTML itself so that wasn't causing any problem so I would say if you have files other than images then you can attachment in a simple text email like this but otherwise just put them in as attachment unless there are images of course but otherwise just use them as proper attachment so that's what I wanted to show you the example in how depending on the email client then it's going to look different so you can see the difference between Outlook and Gmail and it's going to be different for other email clients as well and I can still click on these files so they should open just normal and I think even though document will open I'm gonna blur it out but I'm just gonna go ahead and click on it and you can see that we can see a preview of it or actually we can see the whole thing you can see we can scroll so it's properly displayed because it's added as an attachment and it wasn't embedded in the email body itself so again just to be safe if you want to send embedded files as long as they're images I would say that's safe to do but if they're like other types of file I would just send them as attached just to be safe it should work but as you can see the email client they're having problem displaying the files that are not images so I would just avoid doing that okay so all right let's close out of here I'm just going to go ahead and delete this email because I don't need these emails so now what are we going to do so let's go back to the application we're we're almost done so let's scroll down here go back to the email implementation so we have send attachment to a normal attachment and then we have try to send attachment in the body of the email in this case we saw that it worked mostly for images so we're going to be working on next is this next one so send my messages with embedded files so I think this is a duplicate because you can either pass in images like we're doing for the first two and then you can also pass in a file which the clients the email clients have difficulties displaying so I think what I'm going to do is to just change this to this one so send with files because we're sending all kinds of files and then just get rid of this one because it's definitely a duplicate and we're going to do the same thing in the service so this one can be deleted so now we should be good so for embedded files one more time just make sure that you send embedded images and if it's any other files then make sure you pass it in as an attachment so that's what I would do okay so we're done with this so next what we're going to we working on is this very interesting one which is to send an HTML email so so far everything we've been doing has been sending text emails we haven't been sending any Rich emails that looks like an HTML page that are similar to the ones that you get from you know like Amazon or things like that like with pictures and it just looks almost like a web page so this is why we're gonna learn how to do now we're gonna learn how to send an HTML email that looks almost like a web page when you open the email so the first thing that I want to do is to show you the template that we're going to be using for this because we need to have some kind of HTML and CSS together so that we can send the email and I'm going to go back to the browser and I just kind of like Google like a simple HTML CSS email and then I got this webpage and then I just look at this one it looks pretty cool so I made some modification to it but I'm just showing you where it's coming from because it's not something that I created myself so even if it's free to use you got to give credit to whoever created the the content that you're using but in any case I'm gonna put the hello and in the name of the user at the top here and then some random message and then the button to activate your account is going to be this button and we're going to say you know thank you the support team or whatever one thing I want to bring to your attention is if we scroll down here you'll notice that I'm gonna see if I can zoom in a little more so you'll notice that this is a table so the whole HTML like this whole thing is a tape okay so the whole thing is the table you can see it starts with a tape so the reason for this is because mail servers they're not like a web browser so they can't really parse all the different HTML elements but they can parse tables very well so that's why they make these as as tables so they just kind of style it to make it look like an actual web page but in reality it's really just a tape so that's what I'm going to be using and I already took the code and mixed some small changes to it and I'm just gonna show you what it looks like so let's go back to the application and I'm just gonna go over to the resources so inside of my templates here remember we have time Leaf install so we can use templates I'm gonna right click click and then I'm gonna do new this is gonna be a simple file so we're going to say HTML file it's going to be the email template press enter so now we have this email template that we can use and then I'm just gonna go ahead and just paste the code for it which is the same code that I just showed you with some small changes so let's go over the small changes that I made so here is where we say hello and in the name of the user and since I'm using time Leaf so I get th text which is going to dynamically change the text of this strong tag inside and then by passing in the name so whatever name that we pass in a Java code to this template the time Leaf template engine is going to replace it with this name so it's going to be like hello junior or something and then all of this is hard coded so nothing fancy here and at the bottom here we have the link to verify my account which is the button that you saw and then I do the same so thref and then you pass in the URL the same way so quote dollar sign open curler braces the URL and then close curl braces and timely for just replace this URL to whatever URL you pass in in the Java code and this th is how you invoke the template engine so time left so you do th colon and then the actual HTML property on the HTML element so this is the href and in this case well not really because this would be like text content or something like that or in HTML but in this case it's just text but you get the idea you want to pass in some text and then we set it to this file I'm going to be talking more about this image that's how you embed an image you have to pass in the image tag or any file really and then you pass in C ID for Content ID and then a colon and then the ID of the file that you want to pass so we're not going to be using this yet I'm going to put it in comments okay because we're not going to use it yet but we're going to use the URL and also the name of the user so that's the template that we're going to be using so this is going to be what's going to be sent in the email body with all of these values so now we know that we have to pass in a name and also we have to pass in a URL because those are the name that we're denoting here so we can close this template so the email template.html and then in here we're going to do something very simple so I'm just going to copy everything in here we're going to delete most of it though because this is going to be a little bit different and then go and paste it in here in here okay so first thing we're going to do is we're going to do all this stuff again okay so all of this stuff we're still gonna We're not gonna have any files so I'm gonna remove the attachments part so I'm gonna remove that and then I'm gonna do something called context so we're going to call the context call it context and then we're going to set it equal to a new context okay so I don't think this was imported from the right place I'm gonna scroll up a little more and remove this import that's not the proper import and then I'm gonna scroll down and try to re-import it so go here and then more action see if I can import it again it's supposed to come from time Leaf so let's try to import that so import and let's scroll down and you can see org.timeleaf that context okay so this is the one that we want so this context right here and then we're gonna say context that set variables so we're going to call you can put set variables so there's two so there's this first one you can call it multiple times so we could call this one and then we say the name is going to be the name so the name and the template and then we're gonna pass in the name that we want so we're gonna say user or we already get the name so the name and then we can do it again so we can copy this and then the next one is the URL and then we're gonna pass in the URL so we can see get verification URL we can pass in the host and the token make sure we import this so let's go back here and describe it I'm just going to make it public so make this public and now the error should be gone so we get the verification URL by passing in the host and the token or you can just do contacts that set variable the one that takes a map and then you can do a map of so you can do that as well and then you can pass in the same thing so you can say the first one is going to be the name so you pass that in there and then the second key value pair is going to be the URL so you can just pass this in there as well so you can do it in two strokes like right here or in one stroke so I'm going to comment this out but at least you know that you can do that okay so we're gonna pass in these variables in the context remember that this name so this key here in this URL here are the keys that we have or the variables that we have so the URL and the template and the name and the template so they have to match okay so you have to make sure that you match this key so the name and the URL to whatever key or whatever placeholder that you have in the template so this name here also the URL okay so that's that and then what we're going to do we're going to say string which is going to be the text message we're going to set it equal to we need the template engine okay so that's going to come from well that's the template engine so the template engine that we want to call process and then we're going to pass it the name of the template so email template you can pass in that HTML or you can just leave it like that I don't think it should matter and then you're passing in the context okay so we need to inject this email template so let's scroll to the top and there we go say private final and then we're gonna pass in the template engine so template engine from time leaf and then we're gonna name it template engine okay so that's going to be the dependency injection so this should be good for now okay so we call in the template and then we call Process pass in the template name we can just pass this in as a constant so we can say email template for example so email underscore template okay and then pass in the context which has the name and the URL so that's going to be the text message now that we need to pass into the email so the same way after that we're gonna call the my message called the helper set the priority set the subject set the from set the two and then when we set the text we're just gonna pass in this text so I'm going to say set text and then there's another variable that we can pass in here which is true which means that this is HTML text not just a simple string text so if you go in there you will see that this is the one that we're calling so we pass in the text and then we say it's HTML fast scroll up a little you can see this one where we just pass in the simple text so there doing method overloading so that's all we have to do for this so what we're going to do is to just pass this send email so let's go back to the user service then I'm gonna go down and then we're gonna say email service dot send HTML email so that one here and then they take the same thing so we're gonna pass in all these values pass them in here so I'm going to rerun this and then we're going to just give it a second to come up here I'm gonna go back to the database go back here I'm gonna close this we're gonna delete any confirmation if we had any delete any users if we had any then I'm also going to close my Outlook account we're just going to be using the Gmail at this point and then go back to postman and then send the request again so we're gonna click Send okay so we got another tool one let's go back to the back end just to make sure we didn't get any errors okay so everything looks good so there's no errors now let's go ahead and check our Gmail so I'm gonna go back here and go to my Gmail just gonna go ahead and refresh and you can see it's right here okay so I just refreshed and it just came through and I can go ahead and open that and you can see now it looks exactly the way that we were expecting it to look so you can see pass in my name here and this is just whatever I just type some other stuff here just doesn't really matter so this message is gonna be for you to change and then we have the verify my account so this link if I hover over it and if you look to the bottom left corner you can see that it's pointing to our back end so if I click that then it should verify the account okay so you can see how you can send a rich email that looks like an HTML page and you can add attachment and I'm going to show you that so we're going to add attachments like real attachment we're also going to add another embedded attachment like we're going to add an image inside of this HTML I guess you can see HTML page that we have here okay so that's how you do this so now what we're going to do is to pass in files so now we know how to send a rich HTML email right you can see it here so we know how to do that now we're going to see if we can pass in attachment and embedded files inside of this email so that's the last thing that we have to do so I'm going to go ahead and delete this message and just gonna delete it forever so go in my spam it's deleted and then let's go back to the application so now we're gonna work on this so that we can send file you can add attachment to this the same way that we did on here like you can do this it'll work the same way it will pass in the message body as the HTML and then it will pass in these as attachment so that's pretty easy I mean just for show I can just copy all these and just go down here and we can just paste all this stuff so we're passing in some more attachment and just rerun it so we run just gonna give it a second to start okay so we're up and running go back to the database uh just delete everything in case we had anything I don't remember and then we're gonna go back to postman just send them one more time just so you can see that you can put attachment and let's go back and let's go back to my email let's refresh just gonna give it a moment to come through and you can see the email just came through so I'm gonna open it up and you see that we have the same body and then at the bottom here we have our three files so you can do that if you would like it's gonna be the same thing it's gonna be pretty easy so let's go back to the application one more time so I'm gonna just comment this out but just so you know you can do that as well okay all right so now we're going to do is to work on this right here so let's go again just copy everything here this is going to be a little bit different so so even if we copy we're going to make a lot of changes and then I'm gonna paste it in here so let's work on this right now so what are we gonna do we need to embed images inside of this template right so the first thing that I'm going to do is to enable this so we're going to place the image in here we're going to give it an ID of image which is the content ID so the CID is Content ID we're passing in this one image in here inside of this image tag so that it can be displayed like it would and on HTML so that is that so now I need to go back here so we're gonna get the context we're gonna set all the variables we're going to create the my message actually we can pass this at the beginning I feel like it makes more sense at the beginning so we get the map message get the helper set everything so this text here we're going to set it at the bottom so let's just put this in comments so it's coming that out and then we're gonna get the contacts we're gonna set the variables let me see what I have here okay so I just have the example where you can use set variable so I'm gonna get rid of that because I don't want to confuse you you already know that we have this commented out somewhere here right here okay so if you want to you can see this here okay so back down let me collapse this all right so we're gonna do the same thing we did so get the my message get the helper set the priority set the subject the front email and the to email and then we're gonna get the context We're not gonna set any text so I'll comment this out and then we're gonna get the contacts again pass in this variables so we need the name and the URL which we did before so that's that and then we're gonna process the template by passing in the context which contains the variable that we're passing in inside of the HTML or inside of the template and then I'm going to create something called on my multi-part okay so that's going to be the multi-part and then I'm going to set it equal to a new my multi-part and then I need to pass and relate it so this is the first step and then I'm gonna Define something called a body part so that's going to be the body part I'm going to name it message message body part equal the same thing no my body part okay so now we have a multi-part and also we have a message body part first thing we're going to do is we're gonna take the message body part we're gonna call set content and then we're gonna pass in the text so the text that is coming from the template so this text right here which is literally the whole HTML template but as a string and then we're gonna say this is gonna be text Dot HTML and that's just the type or the encoding so we're gonna say text forward slash text for slash HTML and then let's just do right click refactor go to constant so text HTML we can say encoding okay so that's what that is so we're going to set the content which is the content of the template on the message pad so this message body part here set content and then I'm going to call the multi-part oops multi-part that and then I need to call add bag okay so you can see it takes a body part and you can put an index to see which one comes first or second but in this case I'm just gonna pass in the first one first so that's gonna be the message body so what we're doing technically is we're saying we're going to send an email the first part of it is going to be this Tech which will have you know hello whatever hello so and so and then some random text and then the URL okay so we're passing in here this message body part we pass in the content so this email content to it right here okay so that's the first thing that we have to do so I can add a comment here I can say add HTML email body okay so that's what this is doing this whole thing right here so that's the first body part I need a second body part so I can just copy this so I'm going to copy this and then go down here then I'm gonna say add images to the email body so we're gonna redefine this so I'm just redefined this or re-initialize this message body part I could use a different variable name but I'm going to use the same one because it doesn't really matter because it's being really initialized so I add the HTML and the multi-part which is the email that has multi different parts of it the first part is just the message body which is in this case the text which is the HTM map the second part is going to be the image so I regenerate another message body because I need to add this to this multi-part that's why it's called multi-part because we can pass in many different parts to it so we're already passed in the text which comes from the engine template engine which has the name and the URL okay so hopefully you're following so now we're going to add another body to this email but this time it's going to be a data source so we're going to say data source and then we're gonna set it equal so that's the data source data source and then we're going to set it equal to a new file data source so file data source and then we're going to grab let's grab the dog so let's scroll up here and let's get this dog so we're gonna copy from the system we're just pointing to that particular file on our system so I'm gonna scroll down here and then we're gonna pass it in here okay so that is the new file that we're gonna be adding as a data source and I'm just going to go ahead and see if I can import this so this is data source with a capital S import okay so that's the data source and then I'm going to call this message body part and I need to delete one of these okay so I'm gonna go to go down and then I'm gonna say the message body which is the one that I redefine up here okay I need to call set Handler so this one at the bottom here and I'm gonna pass in a new data Handler and then passing in this data source so we're going to say data source so we're passing in this image inside of this message body remember this is the second message body so the first one was just the text now we're adding in this image to it so after that very important is we're gonna pass in the content ID so here we're gonna say set header so the second one and then this is going to be the content on a dash ID so this is a header so you have to spell it exactly the way that I spell it because it's an existing header in like the HTML and then we're going to pass in the value so in this case I'm gonna pass an image okay so this image has to match this image okay so this image placeholder they have to match with this image okay that's just the content name which is an existing header name and then this is the image that we have to match with the template which is right here okay so that's that so this is the second part and now we need to add the second part to the multi-part okay because we have one here and then we have one here so for that first one that is highlighted we add it to the multi-part so we're going to do the same thing so I'm going to copy this line and then paste it here so this body part here is this new one right here because it's really initialized okay if you want again you can use another variable for this like you just reinitialize it like this and then give it a different name well actually I think I'll just do that that so that don't confuse you very much so I'm going to copy this and then paste it here because I feel like for people that are trying to learn this it's going to be easier to understand so I'm gonna say image by the part okay so that's the image part of the email and then we're gonna pass this in here everywhere and then pass it in the multi part so we create another body part we get the file and then on that body part we set the file and then we pass in this header so that it knows where to put the image and then we edit again the same thing that we're doing here we add this part to the multi-part now this multi-part is initialized here so it's not in the email anywhere which is what we're sending at the very end just let me put a space here so we have one body part that we're adding to the multi-part and then we have another body part that we're adding to the multipart again but the multipart itself you can see it's declared here it's not edited this message in any way shape or form so we need to edit so to do this I'm going to come down here I'm gonna say message oops we want the message so the one from the beginning this message okay we're gonna pass in this multi part which contains the image well first it contains the text the HTML text and then the image so we need to pass this multipart to the image so now we're going to say set content you can see it takes the multipart and then we're going to pass in the my multipart so now when we send this message all of these will be added to that email message so hopefully this is coming together and again if you have any question feel free to reach out to me but this is what we're going to do obviously we're doing all this work in one method but we can easily externalize like this part right here you can have one for a file and one for a text message okay so you can make the method that you're gonna have either take a string or it takes a data source or whatever or something like that but it's really easy to externalize these two parts right here so that you'll have to do this twice like this but we're not gonna worry about this now so this is adding everything to the message we're adding the multi part to the actual email email in the multi-part contain first the text so the HTML text you could also pass in just regular text to this and then you would pass in the encoding as text for slash plane just regular text but in this case it's HTML so we're passing the text.html for the type okay so you can see here the type of the message type okay so hopefully this is coming together and everything makes sense so now what we're gonna do is to just copy this name go back to the email service or user service in this case call the email service that and then passive this name and then pass in the same parameters so we're gonna copy all of this and then paste it in here and then put the semicolon and then make sure we comment this out so that we don't get two of them I'm gonna go ahead and rerun the application and then we're gonna go back to the database well I'm also going to delete this message go back to database delete any confirmation delete any users and also go back to make sure the app is up and running so you can see we're up and running so let's go back to postman and just hit send and just give this a second so everything is successful let's go back application doesn't have any errors okay so it looks good now let's go back to our email let's go back here and I'm gonna oh there it is so you can see we have the no email I'm gonna go ahead and open it up and you can see the image is embedded inside of the HTML so the image is rather large because it's a big image so you can see it inside exactly where we specified that the file should be in the template okay so you can see it right here the way that we're expecting so the image is a little bit too large obviously we don't want to do this so you can see this is spilling over and I have to scroll over even when I scroll over I can't even see the whole pic so let's delete this one more time let's go back to more and go to my trash delete everything forever go back to my inbox and let's just delete everything here we're gonna do one last test so delete and also delete any users and then go back to the application so the first thing I'm going to do is I'm gonna go back and well I need to go back here so inside of this image right so this image we're replacing the image uh hopefully you guys have some HTML knowledge so I'm going to pass in style and then I'm gonna give it a width okay and let's do maybe um let's do maybe 300 pixel so 300 pixel for the width of the image and then the height should adjust automatically and you can pass in more style here if you would like but we're just going to give it a width of 300 pixel so that it doesn't spill over so now I'm going to reload and rerun so let's go back to run okay we're up and running let's go back to postman and go ahead and send this one more time okay so this is successful let's go back to our email well let's check to see if we don't have any errors okay so everything looks good let's go back and go back to our email I'm gonna refresh just give it a second here and there it is okay so there's our email if I open it now you can see we have the text and then we have the dog embedded inside of the HTML we should have put a new line here just so that we can make this go on its own line and then we have our link so if I hover over it you can set the bottom it's gonna take us to verifying the user account and we have our image so let's go ahead and fix that real quick so let's go back to the code and let's go back to the HTML template and then in addition to the width I'm gonna give it uh this uh display of block okay so that it can display on its own line and then push the next element on the new line so let's refresh and let's go back to the browser delete this message go to more trash delete it go back to the inbox close this tab go back here delete one more time okay go back to postman and then send that one last time there we go now go back to my email refresh and there's the new email so I'm gonna click on it and you can see well we should have put some space at the bottom but that's good so now you can see we have this beautiful dog and we can even click on it it will show up pop up here and yeah so that's everything so at this point you should be able to send any type of emails that you would like to send Using a spring boot application and this is really just an example it's a little bit messy so it's up to you to clean things up and make sure that you don't have a very huge method that's doing like many different things but you can do that if you would like so that's all I had and I'm just gonna make sure that I add some space at the bottom here so whenever you get this file you will see that everything displays a little bit better on the UI okay so that's all we have to do and again if you have any questions reach out to me and let me know make sure you check the description so that you can get the source code and this source code is really free to use so you can just take it and then use it in your application it's totally free so there's no problem there and like I said sending email is a big part of any application because you know that almost every institution that you interact with they have a way to communicate with you and one of the most common ways is to use your email address so knowing how to make use of your clients or customers email address or your clientele email addresses is really important and that's what I wanted to show you in this course so you can send simple text email emails with attachments and everything in between and again any questions let me know either reach out to me on social media or join the Discord just ask me your questions there and with that I will see you in the next one
Info
Channel: Get Arrays
Views: 26,653
Rating: undefined out of 5
Keywords: spring boot, spring boot email, java, email, sending email
Id: onCzCxDyR24
Channel Id: undefined
Length: 127min 19sec (7639 seconds)
Published: Sun Jul 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.