[Bug]: Email Message.DeleteOrphanedMessages deletes retrieved (Email Inbox) messages

Aperta Adatta ai principianti
#13,777 1 commento 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
database

Direzione di ricerca

Leggi src/System Application/App/Email/src/Message/EmailMessageImpl.Codeunit.al e individua DeleteEmailMessageIfOrphaned; confronta i controlli per Outbox e Sent Email con il modo in cui le righe di Email Inbox fanno riferimento ai messaggi. Controlla la documentazione di DeleteOrphanedMessages nella codeunit 8904 e i test esistenti sui messaggi email. Il lavoro è completato quando i messaggi referenziati dalla posta in arrivo vengono conservati durante la pulizia e la documentazione descrive questo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Team: Integrations
Describe the issue

Email Message.DeleteOrphanedMessages (codeunit 8904) calls Email Message Impl..DeleteEmailMessageIfOrphaned (codeunit 8905). That procedure treats a message as orphaned when no Email Outbox row and no Sent Email row reference it:

local procedure DeleteEmailMessageIfOrphaned(var EmailMessage: Record "Email Message")
var
    EmailOutbox: Record "Email Outbox";
    SentEmail: Record "Sent Email";
begin
    EmailOutbox.SetRange("Message Id", EmailMessage.Id);
    if not EmailOutbox.IsEmpty() then
        exit;

    SentEmail.SetRange("Message Id", EmailMessage.Id);
    if not SentEmail.IsEmpty() then
        exit;

    EmailMessage.Delete();
end;

Messages retrieved with Email.RetrieveEmails (Email Connector v3, used for example by the Sales Order Agent) are stored as an Email Message referenced only by an Email Inbox row (table 8886). They have no Outbox or Sent Email row, so DeleteOrphanedMessages deletes them, with their attachments, recipients and related records. The Email Inbox row is left pointing to a message that no longer exists.

The rest of codeunit 8905 already treats Email Inbox as an owner of the message:

  • OnAfterDeleteEmailInbox deletes the message when its inbox row is deleted.
  • OnBeforeModifyEmailMessage refuses to modify a message that has an Email Inbox row.
  • The codeunit already has tabledata "Email Inbox" = r in its Permissions.

The XML doc comment on DeleteOrphanedMessages ("Deletes messages that does not have a reference from either the email outbox nor sent email") matches the code, but it predates email retrieval, so I believe the inbox check was missed rather than intended.

I found this while building a cleanup for a customer tenant where custom code had left about 163,000 orphaned messages (about 141 GiB of Tenant Media) behind failed sends. DeleteOrphanedMessages is the only supported way to remove them in Business Central online, because the license gives every user, SUPER included, only indirect access to the email tables. As it stands, a company that also retrieves email cannot run it safely, and there is no way online to see which messages it will delete.

Checked against the current main branch of BCApps (src/System Application/App/Email/src/Message/EmailMessageImpl.Codeunit.al).

Expected behavior

A message referenced by an Email Inbox row is not orphaned and DeleteOrphanedMessages keeps it. Suggested fix in DeleteEmailMessageIfOrphaned:

    EmailInbox.SetRange("Message Id", EmailMessage.Id);
    if not EmailInbox.IsEmpty() then
        exit;

and the doc comment in codeunit 8904 updated to mention the inbox.

A message referenced by an Email Inbox row is not orphaned and DeleteOrphanedMessages keeps it. Suggested fix in DeleteEmailMessageIfOrphaned:

    EmailInbox.SetRange("Message Id", EmailMessage.Id);
    if not EmailInbox.IsEmpty() then
        exit;

and the doc comment in codeunit 8904 updated to mention the inbox.

Steps to reproduce
  1. Set up an email account on a connector that supports retrieval (Microsoft 365 or Current User).
  2. Call Email.RetrieveEmails for that account, or activate a feature that does (for example the Sales Order Agent). Confirm Email Inbox has rows and their Message Id exists in Email Message.
  3. Call EmailMessage.DeleteOrphanedMessages(EmptyGuid, 1000) until it returns an empty Guid.
  4. The retrieved messages are gone from Email Message (EmailMessage.Get(EmailInbox."Message Id") returns false), while the Email Inbox rows remain.
Additional context

BC 27 System Application, same code on current main. Related: Email Inbox has a Retention Policy (my customer's tenant shows it enabled at 2 days), which deletes the inbox row and, through OnAfterDeleteEmailInbox, the message. That is the supported cleanup path for retrieved email, which is another reason DeleteOrphanedMessages should leave those messages alone.

Long term architectural recommendation:

A centralized reference tracking implementation for email message records.

I will provide a fix for a bug
  • I will provide a fix for a bug
Lingua principale
AL
Stelle
720
Fork
488
Merge medio
2g 17h
PR unite (30g)
568

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/BCApps

Tutte le issue di microsoft/BCApps

Issue simili

Altre issue su Databases

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.