Send email with attachment using Spring Boot and Gmail SMTP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up welcome back to another video so in this video i'm going to show you how to send an attachment through email using springboot so for that just go to your favorite browser and search for spring.start.io and just choose the group id so let me call it origin code so just type origin codes or whatever the group id you want to give and just type this artifact id let me call it spin email attachment or you can give anything and just type this description here so i'll just write something like email using spring boot and just add the dependency java mail sender and just click on generate so let me just save inside desktop here and just go to the finder or file location and just double click to extract wherever you want so let me just extract here in desktop and just open my intel ic here so if you are comfortable with eclipse or netbeans you can go with that so let me just click on open and just go to the file location and just click on open so so across the project so this will open the springboot project here so once all the dependencies are installed let me just go into this source main and inside the source so here inside application.properties we need to add the configuration so for that just type spring dot mail dot host equals to smtp.gmail.com [Music] and just type spring.mail dot port equals two five eight seven and also we just need to provide the user name so which is basically a sender email so we will just put the username email here later in the video and just type spring dot mail dot password and also we will generate the app password as well here and just type spring dot mail dot equals to true and also type as spring.mail dot properties dot mail dot smtp and start tls dot enable equals to true so once you do that just go to the sender email so let me just go into this google account here and just click on this manage your google account from your profile and once you do that just go to this security session here so down here you will see the option of two step verification so before generating the app password you just need to do the two step verification by just sending the request into your mobile and configuring by otp so once you do the two step verification just click on this app password here and just enter your password and once you do that let me just click on this select app and click on other custom name so let me just give something like spring email demo or you can give any name and click on generate so you just need to copy this password so just copy this password and you just need to paste it into this password here and also you just need to put the email address here as well so let me just copy my email address from here so you just need to copy this email address so let me just copy and paste it into this username so once this is done let me just create one class inside this package here so let me call it email service and let me just annotate this with service annotation so now let me just create one methods and let me call it send mail with attach meant so let me just type attachment and inside here we will be passing some of the parameters so to email so this is basically a receiver email and just type spring string body so this is the body of email and string subject of email and string so we will be sending the attachment here and inside here we will be using the mime message so for that we just need to use one interface which is private java mail sender so just type java mail sender and we just need to auto wire this interface so let me just type auto wired here and we will be using the my message class so just type my message my message java mail cinder dot create my message and also we will be using the my message helper to set the properties so just type my message helper so just type my message helper and new my message helper so inside here just type my message comma true so down here we will be setting all the properties so just type so you can see it's one error here so we just need to add the exception method here so just click on this add to the method exception and once you do that we just need to set some properties here so just type my message helper dot set from so we need to add here the sender email so which is arjun gotham so you just need to type your sender email here so just type this so which we have just using the application.properties as well so you just need to type the same email address here so once you do that let me just type my message helper again and just type set to and just type two email so this is the receiver email and just type my message helper again and just type set text so this is basically a body of the email and just type my message helper again and just type set subject so this is the subject of an email so once you do that let me just add the file system resource so which is basically for attachment so just type file attachment resource and just type new file resource and just type new file just type the attachment and just type my message alpha again and just type add attachment file system resource dot get file name and just type java mail sender dot send my message so this is showing one error here okay we just need to add one more thing here file system resource so once the mail is successfully sent so we just need to print some message here so for that just type mail with attachment sent successfully or you can give any message so once you do that let me just replace with so yeah successfully so once you do that let me just go inside this spring email attachment application so inside here we just need to auto wire the email sender service so just type auto wired and just type private email sender service so let me just give email sender service here and once you do that let me just create one method again so just type public void trigger email and inside here just type email send the service and also we just need to annotate this with event listener application ready event dot class so just type email sender service dot send email with attachment so inside here we just need to set all the properties so we just need to give two email so which is basically a receiver email so let me just give the receiver email and just give other properties as well so we just need to give the body so just type something like this is body or you can give any body of the email and we just need to give the subject as well so let me give something like this is so let me just type email with attachment and finally we just need to provide the attachment so i have here the resume so let me just go into this download so you just need to copy the file location of this resume so just right click and in case of mac you just type the option and you get the copy option here and if you are using windows or linux you just need to go to the properties and you will be able to copy the file so let me just paste it here and once you do that if i just so you can see some okay you just need to add the exception here so once we do that let me just right click and run my application so you can see the mail with attachment is sent successfully so let me just take the receiver email here so let me just go to safari here and just type gmail so this is my receiver email and you can see here the email with attachment of resume has been received into this email successfully so this is how you send attachment through email using spring boot application thank you so much for watching don't forget to like share and subscribe to my channel
Info
Channel: Code With Arjun
Views: 13,816
Rating: undefined out of 5
Keywords: Email With Spring Boot, Spring Boot, Gmail SMTP, Email with attachment, Java
Id: J0LsMgXfgbU
Channel Id: undefined
Length: 10min 24sec (624 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.