---
title: "IMAP partial fetch truncates attachments"
category: "code"
id: 6fce0f8d-d003-4442-a66e-4949f07f8742
canonical: https://solvedlist.com/solutions/imap-partial-fetch-truncates-attachments
---

# IMAP partial fetch truncates attachments

> IMAP Partial Fetch issue: Attachment truncated due to client decoding Body-Part without size verification. Fix involves reading parts in fixed windows and verifying against BODYSTRUCTURE size.

## Question

IMAP Partial Fetch: Anhang wird abgeschnitten, weil Client Body-Part dekodiert ohne Größenprüfung.

## Solution

Symptom: An attachment with around 5600 announced encoded bytes arrives as a valid-looking but too short file (in the reported case 750 bytes instead of 4096 bytes). The library reports no error.

Cause: `client.download()` decodes the part without exposing the actual raw amount transferred. `meta.expectedSize` is the `RFC822.SIZE` of the entire message, not the partial size. For the decoder, a too short literal is indistinguishable from a regular part end.

Fix: Read the part in fixed windows (e.g., 128 KB) as raw bytes (`fetchOne(uid, { bodyParts: [{ key, start, maxLength }] })`) and verify the sum against the partial size read from `BODYSTRUCTURE`. Any deviation (shorter, early end, excess) results in `INCOMPLETE`/502, nothing is delivered. Then decode strictly: base64 character set, length/padding, expected output length; quoted-printable check every `=XX` sequence; unknown encoding 415 instead of guessing.

Additional: The server must report the window start (`partialOrigins`). If it does not match the requested offset, abort.

Verification: Test against a synthetic server that returns `<0>` on every request. The output length must exactly match the partial size; a shortened window must deliver `INCOMPLETE`.

Note: A library that delivers a shorter file "without error" is not proof of completeness for attachments.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
