All Products
Search
Document Center

Direct Mail:Track email open data by tag using SMTP

Last Updated:Jun 03, 2026

Tag each outgoing email with a named label, then track open and click engagement for that tag in the console — without changing your SMTP sending flow.

Prerequisites

Before you begin, make sure that you have:

  • A tag created in the Direct Mail console. The TagName value in the tracking header must match an existing tag.

  • The SMTP Java call example configured as your sending baseline.

For details on enabling the tracking feature, see How to enable the data tracking feature.

Add dependencies

Add the following dependencies to your Maven project. The code uses Gson to serialize the tracking parameters as JSON and commons-codec to Base64-encode the result.

<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>

Set the tracking header

Build a JSON object with the fields below, Base64-encode it, and attach it to each message as the X-AliDM-Trace header.

Tracking header fields

Field

Accepted values

Description

OpenTrace

"1" — enable; "0" — disable

Tracks how many recipients open the email. Uses an invisible pixel; requires images to be enabled in the mail client.

LinkTrace

"1" — enable; "0" — disable

Tracks how many recipients click links in the email. Replaces links with tracked URLs; works in both HTML and plain-text emails.

TagName

Any tag name created in the console

Groups open and click data under this tag in the console. Must match an existing tag exactly.

Note

The TagName value must match a tag that already exists in the Direct Mail console. If the name does not match an existing tag, tracking data for that message is silently dropped.

Code example

// Set the tracking header to enable open and click tracking.
// For prerequisites and constraints, see "How to enable the data tracking feature".
String tagName = "Test";
HashMap<String, String> trace = new HashMap<>();
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);
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=

After the email is sent, open and click engagement data appears in real time on the console, grouped by the tag name you specified.

What's next