How to Send Emails with Spring Boot: A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the Lambda code in this video we will be exploring how to implement males inducing bimbo we will be utilize the latest version of Sprint Boot and Java 70 and we'll be using Postman for the API testing whether you are beginner or experienced developer this tutorial will guide you through this process of setting up a frame boot application and configuring it to the send mail by the end of this video you will be having working implementation of main send in your spring boot application so let's Dive Right it so now you can open your favorite browser so I will open Chrome browser and search ring initializer so go to this URL this is the spring initializer so we can generate the springboard project in here so I choose Maven this is my favorite so you can select anyone because I recommend you to use Maven configuration so and in this tutorial I will use primu 3.0.5 and also your name as a mail sent and also in this application I use Java 70 so and also you have to add some necessary dependency and web dependency search in name so there are a lot of suggestion so please click spring web so now you can generate project for that click generate this button then we'll download the project go to the open and so you can see this is a project I have downloaded this is a project I have downloaded so now you must describe this code so this is the project I have extracted so you can move this project to your favorite place so I move this project to location so I moved the Border in desktop so then open IntelliJ IDEA so in this project I will be using IntelliJ IDEA but if you want you can use IntelliJ IDEA net bin or Eclipse also but I will recommend you to use intelligent idea because it is much more easy to implement this reboot application so then I have to open the project this is the project I have downloaded the file name mails in so please click home.xml file and click ok and then click open as a project so then the project will open after opening the project please click home.xml file because we have to add some necessary dependencies so you have to add necessary dependency the dependency name is removed data this dependency come from spin framework so after at dependency so you can see there is a icon so please click this icon so then the necessary dependency already at a project so go to the source file and Main and so then we have to add necessary packages for the application right click in here go to new and click packages so I will give you application in master controller and service so in this control controller package I will create another Java class I will give name as email send pin controller in the service package I will create another Java class name email service so actually this email service is a interface and also in service package I will create another package name I am EF this impl package I will create another class name email service I input press enter so in this impl package I will create in the Java class name email service impl and also we have to add annotation call service and we have to implement email service so now go to the emails and controller so we have to add necessary annotation call question is rest press controller turn one is request Mappy so this endpoint I will give a special name called mail if you want to add necessary name so it is Fields free to choose any name in here also so after that so I will implement the info info in mail after that I will test the endpoint using Postman also so in this endpoint actually I will return string so so actually I will return the string so I put a string in here so in return time pass as a history I will give a method name as end in mail so you can use any preferred name so and so in this tutorial I will upload the file and attach the file with email and pin so the file attachment is actually option if you want to add file you can add file if not you don't want to ad5 so the file attachment processes must be optional so I can set as a value I will give file name as a file and required false this two mean two male that receiver mail and and also you can set the CC value it must be array and object and I will give a method name as a end in me so I have to pass the value first one is file to PC object and these are the attribute we have to pass then send email the file upload processor is optional so you can see so IntelliJ IDEA will indicate some error so because in email Services I didn't have a Implement in mail method so you can easily create a method using intelligent idea so you can see there is a create method that's in mail in email service please click this button so IntelliJ IDEA automatically generate this code line if you are using any other editor so you have to write this code in manual so intelligent ID automatically generate this code and also there is another error so you can see one related problem so please hit this link and then navigate to email service impl file is five so you can see there is also so you have to generate necessary code using intelligent idea also please go to the Implement method you click this Implement method URL there will be open in the window after click ok so IntelliJ IDEA automatically generate code for you if you use any idea you have to implement the code in manual but intelligent idea this code will generate automatically it will be saved over time so after that go to the emails and controller so you can see the email service is not highlight so you can see if you put the cursor in the email service so there is a message will be show in the IntelliJ IDEA so in the message it will say private field email Services never signed so you have to add construct a parameter in here so please click this URL so then the IntelliJ IDEA automatically generate Constructor for you so then the controller implementation is done so then go to the configuration file in this project you have to go to Resource and this file name application properties actually prepared working with ML configuration so click this file and go to refactor and rename so then I will rename the file name as so click refactor then double click the application.tml file so you have to have necessary configuration in here so after that you have to add host username password and Port so I will put this value in later so then go to the email service imbl so I have to get from mail so the path is spring Dot me a lot username this is actually this is the spring mail and username so then we have to add another annotation call wired and we have to add Java yeah sender so then I add I'll catch look in here and E I throw the runtime exception in here so then you have to add fine fine message Dot create my message so after that I'm we have to add my message helper new my message helper so in here we have to pass this my message and pass as a true so then we can get our email configuration so that my message helper to set bro this is from mail is a sender mail to the front meal come from in here my message helper it to me other attribute is DC so you can see you can pass the OneNote email address because I pass a salary so if you go to the hit CC method in the IntelliJ IDEA press Ctrl button in clip keyboard then click left Mouse button so then you can navigate the another file so you can see we can pass pcsa string over PCS string array it means you can pass one email address so you can pass more email addresses other attribute is subject for the attribute is a text say text mean text mean the body of the image so then I previously mentioned you can ask one or more file as attachment in the email so you have to iterate the array pass the file one by one you have to add the whole Loop or the at attach file one by one one by one in the email so that I will Implement for Loop by length while length mean how many files you have attached the email there is a method called my message helper Dot add attachment so in here you have to add file name and byte it's mean the data of the file so if you go to the ad attachment method so in here you have to give two parameters one is file name another one is file data so that in this array I will get one file and so there is a another method name called get origin file name so you can see there are many suggestions so you can click the get original file name it means the uploaded file name will get after the second parameter is we have to pass the data of the file there's a method called White array source so you have to pass the file data so there is a method called get bytes so then after that was my message so then actually if the email sending process is success I have to return success message so I return mail send as a message because in this method I set as this string as a return type so this is a mistake you can see in the for loop I I can't remember at at 0 in here so you can see there is no error after adding 0. in this application I use email SMTP server for sending email so that you have to go to your email address and and go to manage your Google account and go to the security tab and then you have to on two step verification so in here you can see the name twist application please click the place and go to and you can see I already added my mobile also in here click continue and see then you can see your phone number will be shown in here so then you can choose as a text message or phone call so and here I use text message and click the send button so then the OTP code will come to the your mobile phone so then you have to put the OTP in here after that click next then you can see there is a button code known so you can see the two step verification is go to the security tab once again so you can see the two step verification is on after that go to the search type password then go to app password so you can select app I will give a custom name removed email so you can see there is a password so you have to keep this password and I will copy the password and go to the application and go to the application.tml file so I paste the password in here The Host name is SMTP Dot email Dot com username is the email address we use so my email address is and Port is by itself both this must be true so our implementation already done so we can test the endpoint using postmap so now I open the postman the postman is a API testing tool so you can use free version of course map I open the postman and I will create a new collection I will give one a message rebootmail demo so this is a folder I have created so I add a request for the test our email application I will gain a message in mail actually this is a post method so our spin moved application front port 8080 because I didn't change the port so the print boot application run before port in 8080 so then we have to get body so in this project I will use home data because we have to upload the file if you want to upload the file you have to use form data so in the postman you can see there is a name text and there is a small Arrow key so the click the arrow key and you have to select file because we have to upload the file and I put the two mail this receiver mail so I put the my email addresses and another email address for cc and after that I add subject and we have to add 4D after that I save and go to the application I run the application so you can see the our application is running code 8080 so then open the postman and you can test the our email so before the send email you can select the file you can see there is a button see that file so when click the file this button so you can upload any file in here so I upload actually small images I upload one images in first time so I click so you can see the file added and Clicks in button so you can see there is a message mails in so it means our email sending application is work perfectly and go to the our email so you can see the email already came to the my email address so in our project we had two email addresses one is two email address other one is easy so after that you can click other email address also so you can see the email received to the set email like this is a email email body and this is a attached file and this is a email subject and this is a send them email so you can see there is a email address we put the PC also so you can see in here congratulations you have successfully learned how to implement mails in using the Sprint boot in this tutorial we covered the necessary step to step upper spring boot application and configure it to send email SLS demonstrated how to use Postman for the API testing with this knowledge you can now incorporate email functionality in your own springboard projects keep exploring and experimenting with different features and library in springboard to expand your skill as a developer thank you for watching and happy coding
Info
Channel: lambdaCode
Views: 14,282
Rating: undefined out of 5
Keywords: SpringBootTutorial, SMSSending, SMTPServer, JavaDevelopment, BeginnerProgramming, SpringFramework, SoftwareDevelopment, CodingForBeginners, DeveloperTips, TechTutorial, ProgrammingTutorial, SpringBootSMS, CodingTips, JavaProgramming, SoftwareEngineering, SpringBootBeginners, LearnToCode, DeveloperCommunity, ProgrammingLanguages
Id: OdQ3GyBsdAA
Channel Id: undefined
Length: 27min 55sec (1675 seconds)
Published: Mon Apr 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.