Create and Send Beautiful User Registration Success Emails with Java and HTML

Amit Gyawali
3 min readSep 1, 2023

In this short tutorial, we will learn how to send user registration emails with the help of Java. Specifically, we will explore how to set up a simple Java application that sends registration confirmation emails to users who have successfully registered.

Prerequisites

Before diving into the code, let’s ensure we have all the necessary dependencies in place. To send emails in Java, we’ll need the following dependencies added to our application’s pom.xml file:


<dependency>
<groupId>com.gyawaliamit</groupId>
<artifactId>spring-html-generator</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.7.1</version>
</dependency>
  • spring-html-generator: This lightweight Java library empowers us to create HTML pages using the Builder pattern, making it convenient to generate well-structured HTML content for our email messages. You can find the latest documentation here
  • sendgrid-java: This dependency allows us to set up an SMTP server, which is essential for sending email, and we can use an API key to connect with the SMTP server. You can follow their tutorial to set up a SendGrid SMTP account.

Setting up the Java Application

Now that we have our dependencies in place, let’s proceed with setting up our Java application for sending registration confirmation emails.

Initialize Your Project

Create a new Java project or use an existing one as per your preference.

Create the Email Content

To craft the email content, we’ll utilize the Spring HTML Generator library. In this scenario, we’ll make use of a pre-existing template prepared for us by Spring HTML Generator. This template can be easily customized to tailor the email’s appearance, incorporate dynamic data, and personalize the message for individual users. The complete documentation is available at the following link: Spring HTML Generator Documentation.

If you wish to make modifications to this template, please feel free to use the code in the documentation as a reference. You can find the code here: GitHub Repository.

public class SendingEmailDemo {

public static void main(String[] args) {

// Generate registration HTML
Template registrationTemplate = TemplateFactory.getTemplate(TemplateName.REGISTRATION_SUCCSS_BASIC);
String htmlBody = registrationTemplate.generate(generateRegistrationContent());
}

public static com.gyawaliamit.spring.html.generator.templates.model.Content generateRegistrationContent() {
com.gyawaliamit.spring.html.generator.templates.model.Content content = new com.gyawaliamit.spring.html.generator.templates.model.Content();
content.setTitle("Welcome to Our Portal");

List<String> descriptionList = new ArrayList<>();
descriptionList.add("Thank you for registering with us. Your account has been successfully registered.");
descriptionList.add("Please feel free to reach out to us for any questions or concerns regarding our services.");
descriptionList.add("To get started, please click the button below:");
content.setButtonContent("Get Started");
content.setButtonLink("https://images.unsplash.com");
content.setDescription(descriptionList);
content.setImageUrl("https://images.unsplash.com/photo-1563694983011-6f4d90358083?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=687&q=80");
content.setImageContent("Image Caption");

List<String> footerNotes = new ArrayList<>();
footerNotes.add("If you have any questions or need assistance, feel free to contact us.");
footerNotes.add("Best regards,<br> Your Company Name ");
content.setFooterNotes(footerNotes);
return content;
}
}

Sending the Email

Now we have our email template ready, we will use SendGrid to send the email to the recipient.

public static void main(String[] args) {

// Generate HTML Content that needs to needs to be sent
Template registrationTemplate = TemplateFactory.getTemplate(TemplateName.REGISTRATION_SUCCSS_BASIC);
String htmlBody = registrationTemplate.generate(generateRegistrationContent());

//provide mail title, to , from, username, and password
SendGrid sendGrid = new SendGrid("YOUR_SENDGRID_API_KEY");

Email from = new Email("your_email@example.com");
String subject = "Hello, World!";
Email to = new Email("receiver@gmail.com");
Content content = new Content("text/html", htmlBody);
Mail mail = new Mail(from, subject, to, content);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sendGrid.api(request);
} catch (IOException ex) {
ex.printStackTrace();
}
}

Once you have set up this code, you will receive the successful email in your mailbox as shown in the screenshot below.

Hope this tutorial was helpful for you, please feel to customize the email content and templates to suit your needs.

--

--

Amit Gyawali

I'm a Software Engineer who's passionate about designing and building things. I enjoy contributing to the community through articles, and open-source projects.