Font Size:
Ask Joget AI

Send Email Attachments Using EmailTool Class

Introduction

This guide walks you through how to configure and send attachments using the EmailTool programmatically by preparing the correct properties object.

Prerequisite

To send an email with attachments using the EmailTool, you need to:

  1. Prepare your SMTP configuration
  2. Define the email recipients and message content
  3. Build an attachment list
  4. Pass all properties into EmailTool().execute(propertiesMap)
  5. Handle success and error scenarios

How does it work?

  1. In Process Builder, click a tool on the swimlane.

  2. Click the Mapping tab and select BeanShell.

  3. In the Script property, insert the script below:
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import org.joget.apps.app.lib.EmailTool;

try {
    // SMTP Configuration
    String smtpHost = "";
    String smtpPort = "465";
    String security = "SSL";
    String username = "";
    String password = "";

    // Email content
    String from = "";
    String to = "";
    String cc = ""; 										// Optional, can be empty string
    String bcc = ""; 										// Optional, can be empty string
    String subject = "Email with Attachment from Joget BeanShell Script";
    String message = "This is an email with an attachment sent from Joget BeanShell script.";

    // Create file attachment
    String filePath = "C:\\Users\\xxx\\xxx\\file.pdf"; 		// Replace with the actual file path
    File file = new File(filePath);

    if (!file.exists()) {
        LogUtil.warn("EmailScript", "Attachment file not found: " + filePath);
        return "Attachment file not found";
    }


    List files = new ArrayList();
    Map fileMap = new HashMap();
    fileMap.put("path", filePath);
    fileMap.put("fileName", file.getName());
    fileMap.put("type", "system"); 							//"system" for loading from the local directory
    fileMap.put("embed", "false"); 							//"true" or "false"
    files.add(fileMap);
    // Add more attachments if needed
    // files.add(fileMap2);

    Object[] filesArray = files.toArray();


    // Email properties
    Map propertiesMap = new HashMap();
    propertiesMap.put("toSpecific", to);
    propertiesMap.put("subject", subject);
    propertiesMap.put("message", message);
    propertiesMap.put("host", smtpHost);
    propertiesMap.put("port", smtpPort);
    propertiesMap.put("username", username);
    propertiesMap.put("password", password);
    propertiesMap.put("security", security);
    propertiesMap.put("from", from);
    propertiesMap.put("files", filesArray);
    propertiesMap.put("isHtml", "true");
    
    // Execute the email sending
    new EmailTool().execute(propertiesMap);

    LogUtil.info("EmailScript", "Email with attachment sent successfully");

} catch (Exception e) {
    LogUtil.error("EmailScript", e, "Error sending email with attachment");
    return "Error: " + e.getMessage();
}
Important
Replace the SMTP credentials, sender/receiver email addresses, and file path with your actual values before use.

Expected Outcome

Once you have implemented the above steps, here is what it would look like after running the process:

Created by Nurkhairini Fitrah Last modified by Debanraj Ravindran on Apr 24, 2026