Message-ID validation for incoming emails

更新时间:
复制 MD 格式

Alibaba Cloud Direct Mail validates the Message-ID header in incoming emails. Emails with a non-compliant Message-ID are bounced.

Note

This validation applies to new users only. Existing users are not affected.

What is a Message-ID

A Message-ID is a globally unique identifier assigned to each email. It serves three purposes:

  • Deduplication: Mail servers use it to detect and discard duplicate deliveries.

  • Tracking: It lets you trace a message through delivery logs and debug failed deliveries.

  • Threading: Email clients use it to group replies and forwards into conversation threads.

Syntax rules for Message-IDs

The following rules define a subset of the requirements for a compliant Message-ID:

  • The Message-ID starts with < and ends with >.

  • The strings to the left and right of the at sign (@) consist of one or more non-empty substrings separated by periods (.).

  • The non-empty substrings can only contain printable ASCII characters.

Compliant vs. non-compliant Message-IDs

Compliant

Non-compliant

Issue

<d52ce63e-a0d5-4f95-b6a9-e1256a44f5fb@example.net>

d52ce63e-a0d5-4f95-b6a9-e1256a44f5fb@example.net

Missing angle brackets

<5ef31701.1c631ghz1.13943.bu15@example.net>

<@example.net>

Empty local part before @

<msg001@example.net>

<>

Empty Message-ID

<msg001@example.net>

<msg 001@example.net>

Space is not a printable ASCII character in this context

The following regular expression validates the preceding rules:

<[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*>

To add comments to a Message-ID, see RFC 5322. For example:

(THIS IS A COMMENT 1)<test@example.net>(THIS is A COMMENT 2)

Where Message-ID appears in an email

The Message-ID is a header field. The following minimal compliant Simple Mail Transfer Protocol (SMTP) message shows its position:

From: "Example Brand" <news@example.com>
To: subscriber@example.net
Date: Mon, 02 Jun 2026 10:15:00 +0800
Subject: Your account update
Message-ID: <20260602.101500.abc123@example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8

Your account update is ready.

Impact of non-compliant Message-IDs

Non-compliant Message-IDs can affect delivery in two ways:

  1. Some email service providers, such as Gmail, require incoming emails to have Message-IDs that comply with RFC 5322. Emails with non-compliant Message-IDs may be bounced or sent to the spam folder. Therefore, optimize the Message-ID when sending emails.

  2. If you send an email to an Alibaba Cloud mail server through an SMTP program and the Message-ID does not comply with the preceding rules, the delivery fails with one of the following errors:

    #Invalid Message-ID:
    564 The format of the message-id is incorrect. Please refer to RFC 5322 section 3.6.4
    #Too many nested comments. For example: (outer(inner)(another comment))
    565 The nested comment depth exceeds the server's support

Validate Message-IDs with Python

The following Python example uses a regular expression to identify compliant Message-IDs in a list of strings:

# -*- coding: utf-8 -*-
import re

def find_message_ids(text_list):
    # Regular expression pattern to match compliant Message-IDs.
    pattern = '''<[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*>'''

    for text in text_list:
        # Find all matching Message-IDs in the text.
        emails = re.findall(pattern, text)

        # Print each compliant Message-ID.
        for email in emails:
            print(f"Compliant: {email}")

# Test with a list of sample strings.
test_text = [
    "<d52ce63e-a0d5-4f95-b6a9-e1256a44f5fb@example.net>",
    "This is a test text, Message-ID.",
    "<5ef31701.1c631ghz1.13943.bu15@example.net>"
]

find_message_ids(test_text)

Pre-send compliance checklist

Before sending an email, verify that the Message-ID meets the following requirements:

  • Starts with < and ends with >

  • Contains exactly one @ sign

  • Both the local part (before @) and domain part (after @) are non-empty

  • All characters are printable ASCII

  • No duplicate Message-ID fields in the same email

  • Nested comment depth does not exceed the server limit