Java Spring Boot Email Sender Service Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to my tutorial where i'm going to show you how to create a simple email sender service in springboot so we're going to start this project by creating an empty spring boot project and you can do that by going to the start dot spring dot io url address and you're gonna get presented with the spring initializer where we can create our spring boot project so now we're gonna leave the project type as may when the language as java and the springboot version we're gonna leave the current lts version which is 2.6.1 now in your case when you're watching this video the version might be different and uh just uh stick with the current selected lts version here and it should be fine for the project metadata we're going to leave the group com.example that can be uh we're going to leave that like that we're going to change the artifact name to email sender and i'm just going to change the package name to be email sender like this i guess the packaging is going to be jar and the java version i'm going to choose 11 because that's what i currently have installed if you have something else that's what you should choose here but currently i have java 11 so i'm going to keep it with java 11. and now let's see what are the dependencies that we're going to need for this project so right here on the right we can add dependencies i'm going to click this and i'm going to choose the spring.web so i'm going to search for the spring web dependency that's one that we're going to need and then we're going to need the mail sender so i'm just going to say java mail sender here and this is the other dependency that we're gonna need in order to be able to send emails and these are all of the dependencies that we're gonna need so we're not gonna need anything else from the dependencies for this project and then i'm just gonna click generate right here this is gonna download a zip file right here and what i'm gonna do is i'm gonna open that zip file and i'll extract this project to the desktop right here and i'm gonna close the zip file and here is where i extracted that zip file right on the desktop now i'm gonna open startup intelligent and with intellij we're gonna open that project so intellij is starting up right now and i'll click open so i'll wait for it to start up and i'll open an already existing project which is going to be this email sender right here so uh just to clarify that so i clicked on the open thing here's and then i'm gonna go and i'm gonna choose uh the so i'm gonna go to my desktop right here and i'm just gonna choose uh i will choose the email sender and you want to choose the one here that has the that that has the thing that identifies that it's uh that it's a project so you want to click this one here on the uh on the outer folder so for example here the other folder of the project is the email center folder and that's exactly where we want to click so you click this thing and you click ok and then you this this asks you do you trust this project because it may have some scripts that are not really good to be uh to be ran for example if you download it from an unknown source but in this case we download we downloaded it from a source that we know uh that we know it's uh from spring and we know that it's used to generate a spring project so we do know where did we download it from and we know that very it's a it's an empty spring with project so that's why we can click trust project in this case and this is gonna open the project in intelligent and it's gonna download the maven dependencies that are specified in the pom.xml file now if we go ahead and check the pom.xml file while it's downloading the dependencies you're going to be able to see that we do have the spring boot starter mail and spring boot starter web right here and those are the two dependencies that we're going to use now let's see how shall we start with this project uh so the first uh thing that you're probably gonna need or the first thing uh that you're probably gonna need is gonna be definitely the uh so you're gonna need uh to create so when you first create a spring boot application what you get is you just get the main class and that's it so now we're gonna we're gonna come to the main package here and we're gonna create a new package and i'm gonna name it service i'm just gonna name it service uh now in the service uh it's we're gonna create uh an interface i'm gonna create an interface and it's gonna be called email sender service and in this service package i'm going to create another package and i'm going to name it impl or that stands for implementation and in this in in here we're going to implement the email sender service now this service is going to consist of only one method and that method is going to return a void and it's going to be named send email and it's gonna receive uh as a string to whom to send the email is gonna receive the subject and it's gonna receive the text or that's gonna be the message so uh or we can yeah we can we can name it message i guess and then we're gonna go in the impl here in the implementation we're gonna create a new class and we're gonna name the class email sender service impl or that stands for implementation we will annotate this class with the service annotation this is going to make sure this is going to make sure that spring picks it up as a bin and creates an object when it starts uh and then we are gonna implement this service so we're gonna implement the email sender service right here and with alt and enter i can click here on this thing and it's gonna tell me that i need to implement some methods and in this case it's just one method so i'm just gonna click that i wanna implement that method now this is exactly where we're gonna implement the method and now let's see how shall we do that so the first thing that we are going to need here is going to be something that's called uh we're going to need to inject something and that's going to be the java mail sender so we need to inject that and i'm gonna name it mail sender that's uh that's already comes from uh from the package we've installed so this spring package installed for the email sending that already comes there and that's why we are injecting that because we are going to use that to send the email so i'm going to add it edit as a constructor parameter and as you can see uh that's now going to be injected here a bin of the an object of that thing is going to be injected in here and we're going to be able to use it now what do we send so in order to send something we need to create a simple mail message and simple mail message would be a new simple mail message this is what the java mail sender can use in order to send an email now we created an object out of this simple mail message now we're gonna fill that object with the data so the simple mail message will receive a from so set from so we can set from which email are we gonna be sending this is where we're gonna put the email but i'm not gonna put it right now i'm gonna put it a little later this is uh where we can also set the two so we can set two and that's gonna be a string actually the two is gonna be from the uh from the function so from the method here we're sending two as a parameter and that's exactly what we're gonna put into the message so we're gonna put here two and then we're gonna set the subject to be equal to what we sent in this method so we're gonna set this object to the subject and the message to the message so simplemailmessage.set text and for the text we're just gonna put the message right here and then all we have to do is call the method so we now all we have to do is just call this dot mail sender and we can just send email we can just click here send and we can send this simple mail message now as you can see what what do we have here let's take a look at it again so if we've injected this java mail sender and we're creating a simple mail message that has a from address we're not we haven't put it yet but we're going to put it later it has a set to address that uh that's the email where we want that's the recipient of the email it has a subject and a message and we are using this java mail sender to send that simple mail message and now what i want to do right now so right now i want to show you uh how can we connect so for example in this case this is not really really gonna work uh because we uh we haven't connected our spring boot application with our own email from which we are gonna be sending all of these emails and in order to do that so in order to do that we're gonna have to go to our application.properties right here uh and uh in order to connect it to the team we're gonna have to add some properties right here and i'll show you exactly what are those properties and how you can add them in just a seconds but before we do that i would like to show you the uh so for example uh before we do that i would like to show you uh how are we gonna use this service so we're gonna use this service in a testing controller and in this case i'm gonna create another package right here and i'm going to name the package it's going to be named controller it's going to be named controller right here and in this controller package what i'm going to do is i will add a java class which is gonna be email uh email sender or email controller let's call it like that and in this email controller we are going to create we're first going to annotate it to be a rest controller because we're going to use this as a rest api and then i'm going to put one post mapping in here and the post mapping can be on slash send email i guess and this is going to be a public it's going to return a response entity and it's going to the method the name of the method can be sent email and now let's uh think through this so the best way to get the parameters here would be to send them as a json in the request body so that's exactly what i'm gonna do here but now it would be really nice if we have some resource class that we can pick up here and just take it uh you know as a json body so that's why i'm going to create another package right here and i'm going to name it resource and i'm just gonna create a class here a basic class that's gonna be named uh email message for example and in this email message i'm just gonna have a private string of two a pri a a private string of subject and a private string message uh i'll right click here and i'll generate uh the default constructors so the default constructor the one with all arguments and i'll just go ahead and do that real quickly here so i'll generate uh i'll generate the one with all arguments and i'll generate getters and setters for this uh resource so let's do that and now we just have a very basic class that just has uh three properties basically a two a subject and a message and we're gonna come to the controller and in the requested body we're gonna say that we're gonna be expecting an email message right here and that's exactly what we're gonna be expecting and now in order to use this email service that we've just created we're gonna have to inject it in our controller and in order to do that i'm gonna have to come to my controller right here and i'm gonna have to inject it so i'm gonna use constructor based injection i'm gonna say private final email sender service email sender service and i'm gonna do alt enter here to add the constructor parameters so that i can use this service in this controller right here and then all i have to do right now in order to use this service it's gonna be to call this dot email sender service dot send email and i'm just gonna say email message dot get to so that's the first parameter the second parameter is email message dot get subject and the other one's gonna be email message dot get message and those are the three parameters that we need to send in here to the service so it does that and then here i'm just going to return a string basically i'm going to return a string and the string is going to be success that we've successfully successfully but we can't just return a string obviously we will wrap it in a response entity dot ok and i'll just wrap a string success like that and so if we successfully send the email we're just going to receive back a string of success so that's going to be our controller and now everything is almost set up the the thing that we have to do is the configuration for the uh for the for our email address so what i have here uh is i already created an account here an email address on gmail and this is the email email sender service tutorial at gmail.com and we are gonna use that email address to uh we're gonna we're gonna connect this email address to our springboot application to be used as the sender email address for this email so that's what i'm going to show you in the next part of this video so now we're going to have to connect our email our google email address our gmail address with our springboot application and in order to do that we're going to go back in our project and we're going to go to the resources folder and to the application.properties file now right here i will paste some parameters and i'll explain them so the first one is the is saying that we're going to be using the gmail snt smpp server this is the port of the server and this is uh this is the auth that we want to use and we want to use the tls that we want that we want the security enabled there now the third and the fourth line are the email and the password and as you can see i didn't uh put them right here in this file because that's not really the best way to do it so i put an environment variable right here so two environmental variables one is the email and the other one is the password and now how can you put values to these environment variables well we first have to run our spring boot applications so i'll run the application it should successfully run and let's just run it because we can we can run it but it's not going to work but we i mean if we try to test it out it's not going to work because we haven't connected an email but we can still run the application and i would then stop the application and as you can see because if you don't run it for the first time we're not gonna have this configuration here but now we do have the configuration and now we can go here and we can click on the edit configurations and then we can go into as you can see here if you choose this configuration we have environment variables and right here is where i'm gonna add the environment variables so i'm gonna add one for the email and i'm gonna add another one for the password now uh i'll add my email right here so that that i can show you because is this email right here oops not i did not mean to do that but i just meant to copy distinct and did i copy it i hope i did let me see uh yes so there's the email and then off camera i'll add my password right here as the values so i'll stop the recording right now i'll add the password and i'll continue with the recording again so i'll stop it right now and right now after i added my environment variables when the application starts this thing here is going to get filled out with email and this thing here is gonna get filled out with the password and now what i'm gonna i'm gonna come back to the email sender service implementation and as the set from address i'm gonna set the email that i have set up in my application.properties right here so that's gonna be the address that we're gonna be sending from and with all of that setup like that we have this service uh successfully built so the service now should work now one more thing you have to do if you're using gmail because gmail by default does not allow for less secure apps to access your account so what you're gonna have to do is you're gonna have to go to your google account and you're gonna have to search for less secure apps access and you're gonna have to enable this thing right here now this is what you have to do if you're using the google email or a gmail account to do this you're gonna have to enable this and now what i'm gonna do is i'll run the application so let's run it and we're gonna test it out and see if it works so i'm gonna run the application we're gonna see if it starts so let's see if it's gonna start up it should start up uh yes it started up right here as you can see and now let's uh see let's go through it once more so what what do we have here we have us we have an interface with one method send email we're implementing that uh method in the implementation where we're injecting the java mail sender and we're creating a simple mail message and we're sending that simple mail message now we're using this service in our controller right here so in the controller right here we're using that service so in the controller we're receiving an email message that's going to be received as a json here in the request body that's why we created this resource right here with all of these three parameters right here and then we're going to receive that in our email controller and we are gonna use the email sender service to send the email and then we're gonna return a response entity okay status and that's what should happen now we're gonna go ahead and test out to see if this thing actually works so in order to test out this api post route i'm going to be using postman and i already filled out the data here as you can see before but what i have here have a post request to localhost 8080 send email i chose here to use the body and i chose the raw format and i chose json here and what i have in the json i have a 2 so i have a parameter 2 which is the to which email descended i'm going to send it to my own email here this is this object so it says this test subject and this is the text but it's not text it's actually message so i should change this because we are expecting here uh the json field to be messaged because this is going to map to as these are here so this is going to be mapped in the json.22 this is going to be the subject and this is going to be to the message so we have to send them with the same name so this has to be two this has to be subject and this has to be message and right now if i but if i'm gonna first open my gmail here as you can see the last email here that this is when i was testing this before is in 523 right here and i'll actually i can even uh uh i'm going to leave them like this so i'm just going to go ahead click the send and as you can see it didn't find the route i guess we maybe messed up something in the controller or something uh nope we didn't mess up i guess before when i was testing it i had it named differently but now it's sent email as you can see so i'll just adjust that right here and i'll just say send email and i'll click again and this time we're gonna get i guess this success and as you can see we got this success string right now and right now if i go as you can see even here i have a notification that i have one message in the inbox and if i come here you can see that right now at 604 pm i have a message received and is the same message so it has the subject here this is a test subject this comes obviously from here and this test message comes from that's the body right here so yeah so yeah guys that's gonna be all for this video i hope you learned something new and see you until the next one
Info
Channel: Ivan Jakimovski
Views: 23,305
Rating: undefined out of 5
Keywords:
Id: XV92kJ8tmmA
Channel Id: undefined
Length: 23min 3sec (1383 seconds)
Published: Mon Dec 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.