Messages
What is a Message?
In the AgentMail ecosystem, a Message is the API-first representation of a traditional email. It’s a structured object containing all the elements you’d expect: a sender, recipients, a subject line, and the body of the email.
Every Message lives inside a Thread to keep conversations organized. When you send a new Message, a new Thread is created. When you reply, the new Message is added to the existing Thread. Literally a normal email thread as we know it.
One of the powerful features of AgentMail is the ability to seamlessly include humans in agent-driven conversations. You can cc or bcc a real person on any message your agent sends, creating a “human-in-the-loop” workflow for oversight, escalations, or quality assurance.
Core Capabilities
You can interact with Message resources in several ways, from sending new Messages to listing a history of correspondence.
1. Initialize the Client
First, you need to initialize the AgentMail client with your API key. This client object is your gateway to interacting with the AgentMail API.
2. Send a New Message
To start a new conversation, you can send a Message from one of your inboxes. This action will create a new Thread and return the Message object.
3. List Messages in an Inbox
You can retrieve a list of all Messages within a specific Inbox. This is useful for getting a history of all correspondence.
4. Reply to a Message
Replying to an existing Message adds your new Message to the same Thread, keeping the conversation organized.
Note that the inbox_id in reply is different from send, in that this is the
inbox_id we are sending FROM. Remember we can have potentially infinite
Inboxes to send from, so we need to tell the api which one we are sending
from.
5. Get a Message
You can retrieve the details of any specific Message by providing its ID along with the inbox_id it belongs to.
Crafting Your Message: HTML, Text, and CSS
When sending a Message, you can provide the body in two formats: text for a plain-text version and html for a rich, styled version.
text: A simple, unformatted string. This is a fallback for email clients that don’t render HTML, ensuring your message is always readable.html: A full HTML document. This allows you to create visually rich emails with custom layouts, colors, fonts, and images.
Best Practice: Always send both text and html versions.
”Why both text and HTML?”
Most modern email clients will display the HTML version, not all of them can render HTML — a text fallback makes sure your message is displayed regardless. Furthermore it significantly improves deliverability.
Styling with CSS
To style your HTML in the Message, you should embed your CSS directly inside a <style> tag in the <head> in the payload of the API request. This is the most reliable method for ensuring your styles are applied correctly across different email clients like Gmail, Outlook, and Apple Mail.
Here is an example of a well-structured and styled HTML header:

Receiving Messages
While you can periodically list Messages to check for new emails, the most efficient way to handle incoming Messages for your agents is with Webhooks. By configuring a Webhook endpoint, AgentMail can notify your application/agent in real-time as soon as a new Message arrives, so you can take action on them.
