{"id":56576,"date":"2023-02-02T11:17:07","date_gmt":"2023-02-02T05:47:07","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56576"},"modified":"2024-01-15T01:47:48","modified_gmt":"2024-01-14T20:17:48","slug":"step-by-step-guide-sending-emails-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/step-by-step-guide-sending-emails-in-spring-boot\/","title":{"rendered":"Step By Step Guide : Sending Emails in Spring\u00a0Boot"},"content":{"rendered":"<h1 class=\"graf graf--h3\" style=\"text-align: left;\"><\/h1>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/0DUfrUQwHKqhJdL92.jpg\" alt=\"\" width=\"320\" height=\"259\" data-image-id=\"0*DUfrUQwHKqhJdL92\" data-width=\"3282\" data-height=\"2658\" data-unsplash-photo-id=\"TaOGbz_S-Qw\" \/><\/figure>\n<p class=\"graf graf--p graf--hasDropCapModel graf--hasDropCap\">\n<p>Using the JavaMailSender interface, Spring Boot offers a simple method for sending emails. You may send an email with Gmail transport layer security by utilizing the spring boot restful web API.<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 0: Add the Spring Boot Starter Mail dependency to your project<\/strong><\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1a_Z-H4WCDeSTphRuN2iJ1A.png\" alt=\"\" width=\"419\" height=\"91\" data-image-id=\"1*a_Z-H4WCDeSTphRuN2iJ1A.png\" data-width=\"419\" data-height=\"91\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1: Configuring mail settings in application.properties<\/strong><\/p>\n<p class=\"graf graf--p\">In the application.properties file, configure the mail settings. Your email server\u2019s host, port, username, and password are all listed here.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1ZetWmAiMVOxUuzh1rosWVg.png\" alt=\"\" width=\"408\" height=\"110\" data-image-id=\"1*ZetWmAiMVOxUuzh1rosWVg.png\" data-width=\"408\" data-height=\"110\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1.0: Generate spring.mail.password<\/strong><\/p>\n<p class=\"graf graf--p\">The general steps to generate a password from Gmail are as follows:<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1.1: Go to Gmail and click Manage your account<\/strong><\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1UQHt2XNl42DU77mmb-BaGg.png\" alt=\"\" width=\"455\" height=\"233\" data-image-id=\"1*UQHt2XNl42DU77mmb-BaGg.png\" data-width=\"455\" data-height=\"233\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1.2: Go to security and turn on the 2-step verification<\/strong><\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1ch2pLeChjNVwNwVZL6V0WQ.png\" alt=\"\" width=\"800\" height=\"284\" data-image-id=\"1*ch2pLeChjNVwNwVZL6V0WQ.png\" data-width=\"1241\" data-height=\"440\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1.3: Click on App passwords and write your app name to generate the password<\/strong><\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1OERpmWZh1CfRhwoATNUWig.png\" alt=\"\" width=\"530\" height=\"324\" data-image-id=\"1*OERpmWZh1CfRhwoATNUWig.png\" data-width=\"780\" data-height=\"477\" \/><\/figure>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1ja_v0Zw36jFbeXeBcadmcQ.png\" alt=\"\" width=\"484\" height=\"312\" data-image-id=\"1*ja_v0Zw36jFbeXeBcadmcQ.png\" data-width=\"770\" data-height=\"496\" \/><\/figure>\n<p class=\"graf graf--p\">Use this password for spring.mail.password.<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 2<\/strong>: <strong class=\"markup--strong markup--p-strong\">Create a service class and inject the JavaMailSender bean<\/strong><\/p>\n<p class=\"graf graf--p\">Use the JavaMailSender to create and send the email using the SimpleMailMessage or MimeMessage classes.<\/p>\n<p class=\"graf graf--p\">A simple mail message typically consists of the message body, subject line, sender\u2019s, and recipient\u2019s addresses. The format could change depending on the email program or service being used.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1fu8YhON__C2cAJZs4Qrp_Q.png\" alt=\"\" width=\"525\" height=\"386\" data-image-id=\"1*fu8YhON__C2cAJZs4Qrp_Q.png\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 3: Call the method to send an email to the desired recipient with the desired subject and message body<\/strong><\/p>\n<p class=\"graf graf--p\">Autowired the EmailService. Here, I am using the example to trigger an e-mail when a seller gets registered. Set the subject and message as per your need. Email will be entered by the user, or you can set it by yourself.<\/p>\n<pre>String Subject = \u201cE-Commerce Application | Seller Account Notification\u201d;\r\n\r\nString Email = seller.getEmail();\r\n\r\nString Message = \u201cHi \u201c + seller.getFirstName() + \u201c, \\n Your Seller Account setup completed \\n\u201d + \u201cWAIT FOR APPROVAL\\n\u201d + seller.getEmail();\r\n\r\nemailService.sendEmail(Email, Subject, Message);<\/pre>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/17xPveDqMh7ncnqPNhda0fg.png\" alt=\"\" width=\"270\" height=\"41\" data-image-id=\"1*7xPveDqMh7ncnqPNhda0fg.png\" data-width=\"270\" data-height=\"41\" \/><\/figure>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1IngpzY8wTtpO7bvzbD3PZg.png\" alt=\"\" width=\"659\" height=\"524\" data-image-id=\"1*IngpzY8wTtpO7bvzbD3PZg.png\" data-width=\"724\" data-height=\"576\" \/><\/figure>\n<p class=\"graf graf--p\">So, as soon as the seller registers himself\/herself, this email will be sent to the specified email.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1DImwbF_um8QV4lDesRYOtQ.png\" alt=\"\" width=\"573\" height=\"124\" data-image-id=\"1*DImwbF_um8QV4lDesRYOtQ.png\" data-width=\"573\" data-height=\"124\" \/><\/figure>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1qyJt8Y7Uk9i-M7GSF2iqyg.png\" alt=\"\" width=\"469\" height=\"400\" data-image-id=\"1*qyJt8Y7Uk9i-M7GSF2iqyg.png\" data-width=\"536\" data-height=\"457\" \/><\/figure>\n<p>Above is the sample email that will be sent.<\/p>\n<p>Feel free to drop down your questions regarding the topic and do explore other blogs on the same or relevant topics.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Using the JavaMailSender interface, Spring Boot offers a simple method for sending emails. You may send an email with Gmail transport layer security by utilizing the spring boot restful web API. STEP 0: Add the Spring Boot Starter Mail dependency to your project STEP 1: Configuring mail settings in application.properties In the application.properties file, configure [&hellip;]<\/p>\n","protected":false},"author":1541,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":568},"categories":[446,1994],"tags":[5097],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56576"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1541"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56576"}],"version-history":[{"count":7,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56576\/revisions"}],"predecessor-version":[{"id":56607,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56576\/revisions\/56607"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}