All Products
Search
Document Center

Direct Mail:Track email open data by tag using SMTP

Last Updated:Oct 29, 2025

For more information, see SMTP Java call example.

Add dependencies

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
     <version>2.8.6</version>
</dependency>   
<dependency>        
    <groupId>commons-codec</groupId>        
    <artifactId>commons-codec</artifactId>        
    <version>1.2</version>   
</dependency>

Modify the related code

For more information, see How to enable the data tracking feature.

// To enable the email tracking service, use the following code to set the tracking header. For prerequisites and constraints, see the "How to enable the data tracking feature?" document.
String tagName = "Test";
HashMap<String, String> trace = new HashMap<>();
// This is the string "1".
trace.put("OpenTrace", "1");      // Enable email open tracking.
trace.put("LinkTrace", "1");     // Enable URL click tracking in emails.
trace.put("TagName", tagName);   // The tag name created in the console.
String jsonTrace = new GsonBuilder().setPrettyPrinting().create().toJson(trace);
//System.out.println(jsonTrace);
String base64Trace = new String(Base64.getEncoder().encode(jsonTrace.getBytes()));
// Set the tracking header.
message.addHeader("X-AliDM-Trace", base64Trace);
// Example value in the raw EML file: X-AliDM-Trace: eyJUYWdOYW1lIjoiVGVzdCIsIk9wZW5UcmFjZSI6IjEiLCJMaW5rVHJhY2UiOiIxIn0=

This feature collects and outputs real-time open and click behavioral data to the console.

For more information, see Obtain sent data for specific conditions using email tracking.