# MicrosoftMails task

A MicrosoftMails task reads a mailbox of Microsoft 365 and saves its messages' attachments, their text, or both, as files. It does the work of the [PopImap](Task-PopImap.md) task for Microsoft mailboxes, through Microsoft's own interface (Microsoft Graph) and not through IMAP. That is the way Microsoft prefers, and it can read any mailbox of the organisation with one registration.

## What has to be set up at Microsoft

The task signs in as an **application**, not as a person. That application is registered once, in the Microsoft Entra admin center:

1. **App registrations** → a new registration. As the redirect address, of the kind **Web**, give the address of this server ending in `register_microsoft_graph_api/`: `https://files.example.com/register_microsoft_graph_api/`.
2. **Certificates & secrets** → a new client secret. Copy its **value** at once; it is shown only then.
3. **API permissions** → Microsoft Graph → **Application permissions** → the permissions to read mail, and to change it if the task shall mark, move or delete messages. Then grant admin consent.
4. Note the **Application (client) ID** and the **Directory (tenant) ID** from the overview.

If the server reaches the internet through a proxy or a firewall, `login.microsoftonline.com` and `graph.microsoft.com` must be allowed.

A change of the permissions later needs the admin consent again.

## Fields

### Signing in

| Field | What it does |
|---|---|
| **Client ID** | the application (client) ID |
| **Client Secret** | the secret's value |
| **Tenant** | the directory (tenant) ID |
| **Get admin consent** | grants the consent from here: you sign in at Microsoft as an administrator. The address of this page must be the one in the registration's redirect address. |
| **Test** | checks the sign-in, and runs the search below against the mailbox, to see that it is written correctly |
| a connection profile | in place of the three fields above: an OAuth [connection profile](Connection-Profiles.md) that several tasks share |

### The mailbox

| Field | What it does |
|---|---|
| **Username** | the mailbox, as its mail address: `orders@example.com` |
| **Based on user id** | the user is named by their id in the directory, not by their address |
| **Folder** | the folder to read. Subfolders with slashes: `Inbox/Orders/2026` |
| **Max messages** | the most messages one run handles |

### Which messages

| Field | What it does |
|---|---|
| **Only process unread messages?** | |
| **Search query** | a fast search across the mailbox, as Microsoft's search understands it. A line that begins with `[SQ]` is written in the server's own simple form, as in the [PopImap](Task-PopImap.md#simple-query) task: `[SQ]from:anna*\|ben* subject:*invoice` |
| **Filter query** | an exact filter in Microsoft's filter language, such as `hasAttachments eq true` |
| **Log only first 10 skipped messages** | |
| **Log Microsoft Graph response** | writes Microsoft's raw answers to the log, for finding faults |

A search finds quickly and broadly; a filter is exact. Use the search to narrow down and the filter where it must be precise. **Test** shows whether Microsoft accepts what you wrote.

### What is saved

| Field | What it does |
|---|---|
| **Download files here** | the folder to save into. **Browse**, **Test**. |
| **Save attachments?** | |
| **Save body of email as file?** | |
| **File Name** | the name of the saved file, with variables: `{mail_sender}_{mail_internetMessageId}_{mail_index}.html` |
| **Filename Filter** | only attachments whose name matches |
| **Minimum file size in bytes** | leaves out signature pictures |

### Afterwards

| Field | What it does |
|---|---|
| **Mark messages as read after processing?** | |
| **After processing copy message to this mailbox** | a folder of the mailbox, such as `Processed` |
| **Delete messages after processing?** | |
| **Route connection through DMZ when possible** | the connection is made from a DMZ server |

## What it hands on

The items it was given, and behind them one item per saved file, with `{name}`, `{path}`, `{url}`, `{size}` and the values of its message: `{mail_index}`, `{mail_sender}`, `{mail_subject}`, `{mail_internetMessageId}` and the other `{mail_…}` values. Put a [breakpoint](Jobs-Breakpoints.md) behind the task once to see them all.

## Things to know

- **The permission reaches far.** An application permission to read mail reads **every** mailbox of the organisation. Microsoft lets an administrator narrow it to certain mailboxes with an application access policy; do that, and give the job a mailbox of its own.
- **Each message once**: unread only, and mark as read; or move, or delete. Marking, moving and deleting need the permission to write mail.
- **The secret runs out.** A client secret has an end date that was set when it was made. When the job starts failing with a sign-in error after a year or two, that is it.
- **Mail is not trustworthy**: see [PopImap](Task-PopImap.md#things-to-know).
- **The log** counts what it did: "Number of messages", then for each one its id, its attachments, and what was marked, copied or deleted.
