Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Email Sender Module

The email-sender module provides a simple utility to send emails using Jakarta Mail (formerly JavaMail). It is designed to be used within the Light-4j framework and integrates with the configuration system.

Introduction

This module simplifies the process of sending emails from your application. It supports:

  • Sending both plain text and HTML emails.
  • Sending emails with attachments.
  • Template variable replacement for dynamic email bodies.
  • Configuration via email.yml (localized or centralized).

Configuration

The module is configured using the email.yml file.

Config FieldDescriptionDefault
hostSMTP server host name or IP address.mail.lightapi.net
portSMTP server port number (typical values: 25, 587, 465).587
userSMTP username or sender email address.[email protected]
passSMTP password. Should be encrypted or provided via enviroment variable EMAIL_PASS.password
debugEnable Jakarta Mail debug output.true
authEnable SMTP authentication.true

Example email.yml

host: smtp.gmail.com
port: 587
user: [email protected]
pass: your_app_password
debug: false
auth: true

Security Note: Never commit plain text passwords to version control. Use Decryptor Module to encrypt sensitive values in configuration files or use environment variables.

Usage

dependency

Add the following dependency to your pom.xml.

<dependency>
    <groupId>com.networknt</groupId>
    <artifactId>email-sender</artifactId>
    <version>${version.light-4j}</version>
</dependency>

Sending a Simple Email

import com.networknt.email.EmailSender;

public void send() {
    EmailSender sender = new EmailSender();
    try {
        sender.sendMail("[email protected]", "Subject Line", "<h1>Hello World</h1>");
    } catch (MessagingException e) {
        logger.error("Failed to send email", e);
    }
}

Sending an Email with Attachment

public void sendWithAttachment() {
    EmailSender sender = new EmailSender();
    try {
        sender.sendMailWithAttachment(
            "[email protected]", 
            "Report", 
            "Please find the attached report.", 
            "/tmp/report.pdf"
        );
    } catch (MessagingException e) {
        logger.error("Failed to send email", e);
    }
}

Template Replacement

The EmailSender provides a utility to replace variables in a template string. Variables are defined as [key].

Map<String, String> replacements = new HashMap<>();
replacements.put("name", "John Doe");
replacements.put("link", "https://example.com/activate");

String template = "<p>Hi [name],</p><p>Click <a href='[link]'>here</a> to activate.</p>";
String content = EmailSender.replaceTokens(template, replacements);

sender.sendMail("[email protected]", "Activation", content);