How to Send email with attachments using Spring Boot | Gmail SMTP | Java Mail Sender

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is one from deod now welcome to this video I'll be sending mail along with attachments using our Javas spring boot applications so for that let's download a string starter project from our string initializer tool for the project I'll select as M languages Java and the spring boot version I'm using this 3.1.3 I'll change the group name to decode now and the artifact name I will change it to Main with attachment uh packaging is jar and version Java version I'll use it as 20 and we need one dependency that is Java main Center [Music] so s yeah so this is all the dependency that we need for this uh project let me just generate the zip let's download it let me me extract [Music] it it's successfully uh you know extracted we have one more video in our Channel where we send mail without any attachments if you want to check it out please do watch the suggested video on the top end of your screen if not let's continue with this video let me open this in my delig so this is our mail with attachment application let me check in the pom.xml whether I have the spring Bo startup made yes I have the uh mail dependency so the next step would be to add some configurations in the application. properties file so let me open resources and go into this application. properties file and the first configuration that I would like to add is springmail do host so uh this is telling what's what's the host name so since I'm using Gmail for this particular video it would be SMTP which is simple mail transfer protocol. gmail.com so this would be my host name and the next uh configuration property would be string do mail. Port under which Port do we want to you know uh send this mail under so that would be 587 and the next property is username uh that is spring do mail do username so this is basically uh from which mail ID do you want to uh send the mail to so that for me would be connect with dcode at gmail.com so you can use your own uh email ID here uh that you want to send a mail from and the next property is springmail do password so this is the password that is required you know for the authentification the dependency that we downloaded the Java you know mail sender will authenticate this particular account using our password so whatever the value we add here will be used for authentication We'll add that value in a second let me add the next uh property spring do M do properties do [Music] msmtp do so this has to be kept true so another property is similar to this which says string do M do properties do mail dotp dot start TLS do enable and this property also has to be set to true so these are these are the configuration properties that we need to set in our application. properties file and I'll tell you how to get this uh password for our Gmail account let me come to my Gmail account open the uh Gmail account from which you want to send the mail to and then in the left side you can see something called security and in the security if you scroll down there is twostep verification enable this twostep verification and then inside if we go it ask for the password let meide the password once you enter this twostep verification page if you scroll down at the bottom we have something called app passwords so just go inside that and here select an app just say custom give your custom name I'm going to say demo name and click on generate so this generates the uh password just copy it and paste it in this password property value so this is basically how you generate password for your mail so username is basically my account you know Gmail ID which is connect with decod gmail.com and password is the value that we generated now after setting all our configurations the next step is to uh write a service Cas so let me open here and create a Jaa class and call it as main service I'll mark this service class with at service and inside this service class I'm going to Auto two things the first uh thing I'm going to a is the uh Java mail sender uh class so I'll just say Java mail sender and give it the name as mail sender and the second thing I want to Auto is uh mail properties so I'll just tell you in a second why I'm taking this I'm Auto wiring this mail properties this this m properties I'll will be using in the method to get this username so in the in the method we have to set from which account we have to send the data right so for that I'll will be using this mail properties we'll see that in a second let me just create a for method saying uh with attachment and this is going to accept few parameters first I would accept a two like to which email that you want to send to and another property for body and another property to set the subject and one more property to say uh the location of the attachment I would name it as file path and inside this uh mail with attachment uh method I would create a new M message so my message I give the name as my message itself and say J mail sender do create my message now I will also use my message helper to set few properties I'll name this name it as helper itself and I would say new M message helper of passing the M message and uh the property that I'm going to set is multiart I set as true that this particular my message is a multiart message and we I'll add an exception to our method signature so here I'm throwing this messaging exception now I will set the values for my helper so I would say helper dot set subject subject is or subject here which we are receiving here in the method parameters and then helper do set from this from value we will get by using this male properties so I will say male properties do get username so the username was this connect with decod gmail.com that we have set so using this mail properties I'm getting that username and and setting it as set from next next I would say helper do set text that would be my body which I have in the method parameters and the last thing we have to set is the two email so for that I would say helper do set 2 and that would be one in the method parameters so now uh this is exactly what we had done in the earlier video that is setting the subject the username the body and the tool the new thing that we are doing here is setting the uh attachment so for that I will use something called file system resource and give it the name as F and I would say new file system resource of new file off and I'll pass the file part so basically this is trying to get the file this file system resource is helping us to get this file from this uh file path so we'll have to mention the file path that is present in our local system and then I'll add this attachment also to our helper so let say helper do add attachment file Dot get file name and also we have to pass the file so while adding the attachment we have to pass the file name as well as the file so now our uh M message is ready adding the attachment or just say main sender do send the M message so for our confirmation I'll just add a system printer then saying that uh mail was sent successfully was sent so yes our service class is now ready we Auto Java mail sender and main properties and then we created a method with which accepts parameters like to which email we want to send to the body the subject and the file path and then we created a my message and set the multiart as true with the help of my message Helper and we set the subject the from address by using this male properties and the text that is the body and the to which email we want to send to and also we added the attachment by using file system resource and finally we sent the main so in order to trigger this let me trigger this from my uh main application for that I main attachment and here I simply Auto the service class which is male service and give it the name as male service and I will create a public method which nothing call trigger name it accepts no parameters and inser I'll just call Ma service do mail with attachment and the things I have to pass is the two email I just pass in the same connect with the Cod at gmail.com and the next thing that I would pass is the body I just say this is body and next thing I I have to pass is the subject so the subject would be demo name with attachment and the final thing is the file path so for me the file path looks something like this uh so this this is just the local file know directory path uh where I'm where I'm just trying to send this txt file so this is all I need there is an error it says add exception to the method signature so we throw this messaging exception here also so the same thing we are doing it here and let me just add add event listener and inside pass application ready event. class yeah so now I'm just triggering the mail uh this uh service class from my main application let me just run my main class so the conso I can see see mail send exception it says that uh there is an issue with this uh start PLS command so that would be with respect to uh the application. properties so yeah so here if you see there is an there is you know a here so properties do mail do SM SMTP dots. enable so I fixed that hopefully it should run now let me rerun the application and if you see uh I got the message mail was sent successfully let me check in the mail whether that was the case Let me refresh and I have received a mail saying uh this body uh and you know and the subject is demo mail with attachment and I have received the attachment so so this is how you send mail from Java springbot application along with attachment so if you have learned something new please do subscribe to our Channel and support us if you have any questions suggestions or request please do 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: 1,596
Rating: undefined out of 5
Keywords:
Id: 6xqKgHxlizk
Channel Id: undefined
Length: 17min 35sec (1055 seconds)
Published: Wed Nov 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.