How to Send Email Using Spring Boot | Gmail SMTP | Java Mail Sender | Step by Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is Suman from D code now in this video we'll be learning how to send mails using spring boot so let's have a look at our to-do list so first we have to download the starter package with required dependencies in order to start with this uh sending mail feature so let's go to browser and type in Spring initializure go inside the start.spring.io the project I would be selecting task Maven language is Java spring boot version is 3.1.1 and the group name I'll change it to decode now and the artifact name I'll change it to mail them packaging would be jar and the Java version for me is 20. on the right hand side we can add dependencies so here I'll be adding three dependencies the first dependency is with respect to Spring web this helps us with handling all the HTTP requests and creating the servers and stuff so we need this dependency for that and the one more dependency is called lombok so this reduces the boilerplate code and makes our life easier for that reason we need this dependency and last dependency is the male dependency so this is the Java mail sender dependency which helps us in sending the mail so we need to add this dependency let me click on generate so the mail.d demo is downloaded let me go to downloads extract it go to desktop record now I have created a folder called me I'll just extract it there so this is the starter package I'll just open this in my intelligent mail demo application let me go to Source main Java and this is the mail demo mail demo application so now let's take a look at our to-do list downloading the startup package is completed so let me Mark with us completed and the next thing we have to do is add necessary configurations to the to connect to the main server so uh in our resources we have a file called application.properties just go inside that and here we have to add the configurations to connect to our mail server so the first configuration is spring dot mail let me just copy this spring dot Main mail.host which is equal to SMTP that is simple mail transfer protocol dot I am using Gmail to send the mail so for me it would be gmail.com the next configuration would be string dot mail dot Port here we just mentioned the port number which is 580 7. next would be spring dot mail dot username so username is nothing but the mail ID using which we want to send the mail for me it looks something like this it's Balaji eir this is just a dummy email ID at gmail.com and next we have a password so how can we set this particular password so for that uh let's jump into our Google account and uh see how to create this uh password new tab I'll add the link for my Google account so this is my Google account the mail ID which I mentioned there and on the left hand side we can see something called security go inside that and if you scroll down we can see this two-step verification if this is not on for you just uh turn this to sub verification on go inside that we need a password for that let me just add my password I'll be signed in and at the bottom of this page there is something called app passwords go inside that and you can see here uh here is the this is the place where we can generate the app passwords I'll just select the custom name I'll name it as spring mail and click on generate so this is our app password it will only be available now so just copy it and go to the application.properties and paste it in the fourth line where we have mentioned spring.mail.password so this is the username and this is the password so our next configuration is spring dot mail dot properties dot email dot SMTP dot path so this uh simple made transfer protocol authorization should be set to proved and similarly we have one more configuration that we want to set it as to uh which is the start PLS start PLS should also be set to prove so these are all the configurations that is required for to connect to the mail server so let's jump back to our to-do list and Mark this as completed and the third thing we want to do is Define a model for our main structure so let me come back to the code and create a package called model and inside this model package I'll create a class called mail structure so what this mail structure uh contains is it just contains one string which is let's call it as subject subject for our mail and the other field is that is also a string that would be our message so these are the two Fields present in our mail structure I'll just add the at getter and Etc annotations so these are the two annotations which are provided by the long book dependency which we added while downloading the starter project so this is the mail structure model Plus so let's mark this as completed and the next thing we want to do is create a controller method to call the service class so we need we need a controller package so let me create a package called controller and inside this controller package I'll create a new class called mail controller so for this mail controller we have to add a annotation called address controller so if you want to learn about these annotations and all the fundamental concepts of building a springboard project that's a separate lecture series on building a spring boot application to perform credit operations uh please check that out we also need one more annotation called at request mapping and inside that I'll give the slash mail so now let's create a method to call the service class so this particular method would be something like and it would return a string uh it would say something like deleted successfully sorry the mail send successfully or failed to send the mail something like that just it returns a string and I'll keep the name as send mail it will accept two parameters one param the first parameter is the mail ID to which we want to send the mail to so that I'll that we will be sending it in the path variable and it is of type string and I'll just give the name as mail and also we'll be accepting one more parameter that is the request body in the request body will be accepting uh in the request body a model of type mail structure so it's male structure and I'll just give the name as main structure itself so this is our send email controller method I'll add some annotations for this we need since this is a post method we need something called post mapping and inside let's say send slash the path variable which is our email so uh this is our controller method and the URL is looks like this and mail is our path variable and in the request body uh they would have sent something like the subject and the message they want to send in the mail so now from this controller method I want to call the service method so let me just come to our to-do list and Mark this controller thing as completed and the next thing we have to do is create a method in the service class so before creating the service class let me create a service package so this is my service package under the service package I am going to create a class called mail service and I'll mark this uh with The annotation service and inside here I'll create a method to send the mail for that I will say something like public and it returns nothing it just sends the name so return type is one and then name would be something like send mail and it accepts two parameter one is the mail which is present in the path variable and the next is of type mail structure which we will we will be getting in the request body so these are the two parameters that are accepted by our service method send made so in order to send this mail we have to auto air something called Java mail sender so let me use The annotation at Auto wired and say private Java mail sender give it the name as mail sender so this particular class helps us in sending the name and inside our method I'll say something like simple mail message and give it the name simple mail message and say new upgrader I'm creating just an object of simple mail message I'll then add simplemail message dot set we can add so many things here we can set the BCC from subject text so let me add from so the from should be the mail ID from which we are sending uh the name which is present in our application.properties in the username so this particular value which is present in the application properties that can be taken from uh taken into our service class by using at Value annotation I'll just say at Value uh inside double quotes dollar bracket whatever is present in this application.properties which is spring dot mail Dot username I'll just say spring dot mail dot username and close the brackets and we can give it give a name here saying that it's a string I'll just say make or I already have the mail so I'll just say from mail and similarly we can get the password also but password is not needed here all the authentication stuff is taken care is taken care by this particular Java mail sender class and here I will just mention from me similarly we can set the other parts of our simple mail message we can set the subject so I'll set the subject to the subject which is present in the request body which is present like mail structure Dot get subject and with respect to the body I'll set the simple mail message dot set body or the set text which is present in our mail structure dot get message so now we have set the from address the subject and the text so we need to add one more field like to whom do we need to send this particular mail message to set to that would be present in our path variable so that is mail so now our simple mail message is created now we just have to send it so that we can do by using this uh class which is Java mail sender I'll just say something like mail sender dot send and the messages our simple mail message so with this uh it will automatically send the name so this will send the mail from our email address which is present in the application.properties uh get the subject from the request body and also the message from the request body and to which email does it send to it will send the mail to the mail ID which is present in our path variable so this is the service method which we are using let me just go to controller class and call this particular method for that we need to Auto wire our service class let me whatever private the name of our service class is mail service will be the name as mail service itself so now I'll call within the controller method made service dot send mail and it is expecting two parameters one is the mail and then the other is the mail structure so I am sending both the things and after that I'll just say return successfully send the me so this completes the controller method as well as the service method let's look at our to-do task so creating a method in the service class is also completed it will mark it as completed now we just have to send the name for that let me open personal or before that let me just run this particular application foreign the type is post URL would be localhost 8080 slash parallel looks something like this it starts with mail and then send under mail ID okay so it starts with mail slash send slash we need to uh to which uh mail ID should we send the email to let's say connect with decode so if you want to contact us just uh send mails to this particular mail ID which is connect the connect with decode at gmail.com it's expecting a request body which contains the subject and the message let me check the name again it's subject and message so subject would be something like testing comma message which is the Main so these are the two things required and if I go to headers and change this content type enter it type to Json applications like Json and then hit send so it is throwing a 500 internal server let's take a look at the logs okay we have some exception here the send failed exception must issue a start PLS command first so in our application.properties we have mentioned this start PLS to be true okay it should be start PLS dot enable so just add this dot enable in the sixth line of our of your application.properties and restart the application hope this should work feet so it's up and running let me go to postman and read the end part again successfully send the mail so let's go to my Gmail and check whether I have received this particular map so as you can see here I received the mail so we have received the mail but here it says spring dot mail dot username so at Value annotation is not working properly so let's go to the core and check that if I come to my mail service so this is the art value annotation which is present so it's a syntax error will have to mention it within flower brackets instead of the small brackets let me just rerun the application again foreign let me go to my personal and hit this end point again successfully send the mail so let me come to my mail inbox and there's one more mail and here now before you are getting spring dot mail dot username after the syntax correction I'm receiving as the uh as expected so let's come to our to-do list and sending the mail is also completed so so this was all for this video if you have learned something new please do subscribe to our Channel and support us if you have any questions suggestions or requests please let us know in the comment section below and don't forget to like the video see you in the next video
Info
Channel: Decode Now
Views: 6,843
Rating: undefined out of 5
Keywords: #mail, #gmail, #springboot, #javamailsender, #javamail, #springbootmail, #sendmail, #sendmailfromspringbootapplication, #howtosendmailfromspringboot, #sendgmail
Id: FYq9yWgrO8o
Channel Id: undefined
Length: 20min 54sec (1254 seconds)
Published: Wed Sep 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.