Skip to main content

Posts

Sending Stunning HTML Emails in Spring Boot 3.x with Thymeleaf

In this guide, we'll walk you through how to send dynamic and responsive HTML emails using Spring Boot and the Thymeleaf template engine . By the end, you’ll have a fully functional system that sends personalized emails right from your Java backend. 📁 GitHub Reference You can explore a complete working example on GitHub here: 👉  Html-Email-Spring-Boot by TheDipDeveloper 🚀 Project Setup: What You’ll Need Start by creating a Spring Boot project with these dependencies: Spring Web Spring Boot Starter Mail Thymeleaf Lombok Add These to pom.xml : < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-mail</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-thymeleaf</ artifactId > </ dependency > < depe...

How to Send Emails in Spring Boot Using SMTP (With and Without Attachments)

Sending emails is a common requirement in modern web applications — for things like user registration, password resets, or notifications. In this tutorial, we’ll walk through how to send emails in a Spring Boot application using SMTP , specifically with Gmail’s SMTP server , and demonstrate how to send both plain emails and emails with attachments . 📺 Video Demo If you prefer watching over reading, here’s a full demo of this tutorial in action: 📁 GitHub Repo  Want the complete working code? Clone the GitHub link provided below which contains all the source code. Source Code GitHub Link: https://github.com/TheDipDeveloper/Spring-Boot-Sending-Email 🧰 Prerequisites Java 17 or above Maven Spring Boot 3.x A Gmail account  🚀 Step 1: Add Spring Boot Mail Dependency First, add all the required dependency on pom.xml file < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring...

Different ways to create Optional objects in Java 8

 In Java 8,  Optional<T>  is a container object used to represent the presence or absence of a value. It's commonly used to avoid  null  checks and  NullPointerException .  Below are different ways to create an  Optional  object in Java 8: 1.  Using  Optional.of(T value) Creates an  Optional  with a non-null value. String name = "John" ; Optional< String > optional = Optional.of( name ); 🔴 Throws  NullPointerException  if  name  is null. 2.  Using  Optional.ofNullable(T value) Creates an  Optional  that may hold a null value. String name = getName(); // could return null Optional< String > optional = Optional.ofNullable( name ); ✅ Safe way to wrap possibly null values. 3.  Using  Optional.empty() Creates an explicitly empty  Optional . Optional< String > optional = Optional.empty(); ✅ Used to represent absence of a value clearly. 4.  Using...

How null key is stored in HashMap Java 8+?

  In Java, HashMap has specific behavior when it comes to handling null keys and values: ✅ null Key Handling in HashMap A HashMap allows one null key . Internally, the null key is treated specially and stored in the first bucket (index 0). If you insert another entry with null as the key, it will overwrite the previous one. ✅ null Values A HashMap allows multiple entries with null values . 🔍 Example: import java.util.HashMap ; public class NullKeyExample { public static void main ( String [] args ) { HashMap < String , String > map = new HashMap<>(); map .put( null , "first" ); // First null key map .put( "one" , "1" ); map .put( null , "second" ); // Overwrites the previous null key System . out .println( map .get( null )); // Outputs: second System . out .println( map ); // Outputs: {null=second, one=1} } } ⚠️ Notes: If you use other Map...