MailWorker lets your build an agent that communicates via email. It receives incoming messages, stores them in your room, generates replies using an LLM, and sends responses back out through SMTP — automatically turning your inbox into an intelligent, room-connected agent.
MailWorker extends the Worker agent to process inbound email from a queue and send replies via SMTP. It automatically persists every message and attachment to room storage, builds conversation context from past emails, and can call tools or reason over messages like any other agent.
Prerequisite: To use MailWorkers you must either be an admin of the MeshAgent project you are using or have an admin create and allocate an email address for the agent to use. Only project admins can create and allocate email addresses.
When to Use It
UseMailWorker when you want an agent that:
- Responds to incoming email automatically (e.g., helpdesk, order confirmations, triage bots)
 - Maintains threaded context so replies include past conversation history (based on the current email thread)
 - Runs LLMs or tools over incoming email messages before responding
 - Bridges email and your MeshAgent environment, connecting to other agents or toolkits in the same room
 
Constructor Parameter
MailWorker inherits all Worker parameters (like queue, llm_adapter, tool_adapter, toolkits, rules, and requires) and adds a few email-specific ones:
| Parameter | Type | Description | 
|---|---|---|
queue | str | Name of the queue to listen to (default "email"). | 
email_address | str | The address used as the “From” field for outgoing replies. | 
domain | str | Domain for message routing, defaults to MESHAGENT_MAIL_DOMAIN or "mail.meshagent.com". | 
smtp | SmtpConfiguration | None | SMTP settings (hostname, port, username, password). Defaults to environment variables if not provided. | 
Lifecycle Overview
MailWorker inherits its lifecycle from Worker:
await start(room): connects to the room and begins long-polling the email queue for new messages.await stop(): gracefully stops the polling loop and disconnects.
Note: “Long-polling” means the worker efficiently waits on the queue until a new message arrives—no busy-looping or manual sleep required.
Processing Flow
MailWorker runs a continuous loop inherited from Worker that listens for incoming email messages on a queue and processes them end-to-end.
- Receive an email — The worker long-polls the email queue and receives base64-encoded .eml messages.
 - Parse and persist — It decodes the message, extracts headers, body, and attachments, and saves them under 
.emails/...in room storage. A summary record is inserted into theemailstable for lookup and threading. - Rebuild thread context — The agent walks the 
In-Reply-Tochain to include earlier messages in the LLM’s context. - Generate a reply — The 
llm_adapteruses the chat context and toolkits to produce a text reply. - Send via SMTP — A threaded reply (
RE:, In-Reply-To, Message-ID) is composed and sent using your SMTP credentials. The reply is also stored alongside the original message. 
Key Methods
| Method | Description | 
|---|---|
process_message(chat_context, room, message, toolkits) | Core processing logic—decodes, saves, builds thread, calls the LLM, and sends the reply. | 
append_message_context(room, message, chat_context) | Loads the full email thread into the chat context. | 
get_thread_toolkits(thread_context) | Resolves local and required toolkits for the current email thread. | 
send_reply_message(room, message, reply) | Composes and sends the actual reply message via SMTP. | 
Example
First create and allocate an available email address, then define aMailWorker that uses it.
meshagent project list from the CLI to get the IDs associated with the MeshAgent projects you have access to.
maildemo room and listening on the queue you configured. As mail arrives (forwarded into the queue), the worker will parse, store, generate a reply, and send it via SMTP.
Note: Once you stop the room, the mail agent will not automatically restart unless you save the agent configuration and deploy the agent as a project wide service or run it via a room container. To persist your mail agent and reuse it you will need to save the configuration for your agent and create a project wide service that manages saved agent configurations via room containers. This project wide service will be responsible for starting/stopping agent configuration in a specific room. You can learn more about this in the services & containers documentation.
Next Steps
Explore and understand other agents in MeshAgent:- Worker: Understand how queue based agents work
 - TaskRunner: Learn how to run agents in the background or wrap existing agents from other frameworks
 - ChatBot: Create a conversational text/chat based agent
 - VoiceBot: Create a conversational speech/voice based agent
 
- Services and Room Containers: Understand agent deployment patterns