# MailDeltaFetch

### Summary

* Combines Microsoft Graph delta tracking with full message retrieval so clients receive every new or updated mail (including body content) since their last stored delta token.

### Graph Endpoint

* `GET /me/messages/delta`
* `GET /me/messages/{id}`

### Parameters

#### Required

* `mailDelta: string` — Delta token from a prior `MailDeltaFetch` or `MailInitDelta` call. Use this to resume change tracking.

#### Optional

* `deltaLink: string` — Legacy alias for `mailDelta`. Only one of `mailDelta` or `deltaLink` is needed.
* `folder: string` — Folder ID or well-known name (e.g., `Inbox`). Used when starting without a token or when the token becomes invalid.

### Usage (app.businessFunction)

#### Minimal

```js
app.businessFunction({
  functionName: 'MicrosoftGraph',
  methodName: 'Mail',
  operation: 'DeltaFetch',
  parameters: {
    mailDelta: storedToken
  }
})
```

#### With options

```js
app.businessFunction({
  functionName: 'MicrosoftGraph',
  methodName: 'Mail',
  operation: 'DeltaFetch',
  parameters: {
    mailDelta: storedToken,
    folder: 'Inbox'
  }
})
```

### Response

* Returns the full messages (same shape as `MailGet`) plus the next delta token to persist for the following call.

```json
{
  "items": [
    {
      "id": "AAMkAGVmM...",
      "subject": "Quarterly report",
      "from": {
        "name": "Finance",
        "address": "finance@contoso.com"
      },
      "toRecipients": [
        {
          "name": "Me",
          "address": "me@contoso.com"
        }
      ],
      "receivedDateTime": "2025-01-16T08:22:11Z",
      "isRead": false,
      "body": {
        "contentType": "HTML",
        "content": "<p>Attached you will find...</p>"
      },
      "webLink": "https://outlook.office.com/mail/inbox/id/AAMk..."
    }
  ],
  "deltaLink": "https://graph.microsoft.com/v1.0/me/messages/delta?$deltatoken=..."
}
```

### Notes

* Use `MailInitDelta` to bootstrap the first `mailDelta` token. Afterwards, persist the `deltaLink` returned by each `MailDeltaFetch`.
* Messages marked as removed by Microsoft Graph are skipped; clients should continue to rely on `MailDelta` if they need delete notifications.
* If the delta token is expired or invalid, the operation throws a friendly error so callers can fall back to `MailInitDelta`.
