microsoft / microsoft/BCApps

[Bug]: SMTP Email: strip invalid XML declaration injected into HTML body after inline-image → CID conversion

Open Beginner friendly
#10,144 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Team: Integrations
Dominant language
AL
Stars
683
Forks
459
Avg merge
3d 26m
Merged PRs (30d)
633

Description

Describe the issue

In [SMTPMessageImpl.Codeunit.al], procedure ConvertBase64ImagesToContentId() parses the HTML body into an XmlDocument and re-serializes it via Document.WriteTo(WriteOptions, DocumentSource) whenever the body contains at least one element (e.g. inline base64 images). The AL XmlWriteOptions type only exposes PreserveWhitespace() and offers no way to suppress the XML declaration or control its encoding. As a result, the serialized string always starts with , regardless of the actual wire encoding. This string becomes the new MimeBodyBuilder.HtmlBody, which MimeKit then transmits as Content-Type: text/html; charset="utf-8" with Content-Transfer-Encoding: quoted-printable. The in-document XML declaration therefore contradicts the actual MIME charset. Outlook's Word-based rendering engine gives precedence to the (wrong) in-document declaration over the MIME charset, interprets the UTF-8 bytes as UTF-16, and the resulting garbled text gets misclassified as Japanese by Outlook's language/encoding auto-detection.

Expected behavior

An HTML body that is embedded as a MIME text/html part must never contain an XML declaration/prolog — it's an HTML fragment, not a standalone XML document, and any declaration inserted by XmlDocument.WriteTo cannot reliably reflect the actual transport encoding chosen later by MimeKit. The prolog should be stripped from DocumentSource before it is assigned to MimeBodyBuilder.HtmlBody

Steps to reproduce

Send an email via the SMTP connector/API with SetBody(HtmlBody, true) where HtmlBody contains at least one .
Inspect the raw .eml of the sent message.
The text/html MIME part's body starts with even though the part is declared and transferred as charset="utf-8" / quoted-printable.
Open the email in Outlook the message language can be auto-detected as Japanese even if the language of the mail is e.g. german

Additional context

Original HTML body of the mail with the base64 image
HTMLMaiBodylWithBase64Image.txt

Received Mail from OWA (Downloaded .eml file)
Mail.eml

I will provide a fix for a bug
  • I will provide a fix for a bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in SMTPMessageImpl.Codeunit.al at ConvertBase64ImagesToContentId(), focusing on the Document.WriteTo call and the assignment of DocumentSource to MimeBodyBuilder.HtmlBody. Reproduce the case with SetBody(HtmlBody, true) and an inline base64 image, then inspect the raw .eml. Done means the text/html part has no XML declaration while retaining its expected UTF-8 and quoted-printable MIME metadata.

Written by the indexing model from the issue text.

Assessment

Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.