Sending and deleting
TTMSFNCCloudMicrosoftOutlookMail composes and sends messages, either from
loose subject/body/recipient values or from a fully-populated mail item, and
deletes existing messages. This chapter covers both send overloads, sending
as an application rather than as the signed-in user, and deleting a message.
Sending a message
SendMessage sends a message built from a subject, body, recipients, and
optional Cc/Bcc lists and attachments; AMailType selects plain text or
HTML. An overload sends an already-populated TTMSFNCCloudMicrosoftOutlookMailItem
directly. SendMailMessage is an alias for SendMessage with the same
overloads — use whichever name reads better in your code. Both raise
OnSendMessage when the send completes.
procedure TForm1.SendStatusReport;
var
Recipients: TStringList;
begin
Recipients := TStringList.Create;
try
Recipients.Add('recipient@example.com');
TMSFNCCloudMicrosoftOutlookMail1.OnSendMessage := OutlookMailSent;
TMSFNCCloudMicrosoftOutlookMail1.SendMessage('Status report', 'Everything is running fine.', Recipients);
finally
Recipients.Free;
end;
end;
procedure TForm1.OutlookMailSent(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
ShowMessage('Message sent')
else
ShowMessage('Send failed: ' + ARequestResult.ResultString);
end;
Sending as an application
Passing a value for AApplicationUserIdOrEmail sends the message using the
app's own application-level Graph permissions on behalf of the specified
mailbox, instead of the delegated permissions of the currently signed-in
user — useful for a backend service that sends mail from a shared mailbox
without a user ever signing in interactively.
procedure TForm1.SendFromSharedMailbox;
var
Recipients: TStringList;
begin
Recipients := TStringList.Create;
try
Recipients.Add('customer@example.com');
TMSFNCCloudMicrosoftOutlookMail1.OnSendMessage := OutlookMailSentAsApplication;
// AApplicationUserIdOrEmail selects the mailbox to send from; requires an
// application-level Mail.Send permission with admin consent.
TMSFNCCloudMicrosoftOutlookMail1.SendMessage('Your order has shipped',
'Track your package using the link below.', Recipients, nil, nil,
mtPlainText, nil, 'support@yourcompany.com');
finally
Recipients.Free;
end;
end;
procedure TForm1.OutlookMailSentAsApplication(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
ShowMessage('Message sent from the shared mailbox');
end;
Deleting a message
DeleteMessage removes a message, either by passing the retrieved mail item
or its identifier, raising OnDeleteMessage when the deletion completes.
procedure TForm1.DeleteMessageByID(const AMailItemID: string);
begin
TMSFNCCloudMicrosoftOutlookMail1.OnDeleteMessage := OutlookMailDeleted;
TMSFNCCloudMicrosoftOutlookMail1.DeleteMessage(AMailItemID);
end;
procedure TForm1.OutlookMailDeleted(Sender: TObject; AMailItemID: string;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
Memo1.Lines.Add('Deleted message ' + AMailItemID);
end;
Combining a send with a follow-up delete
Deleting a draft item immediately after successfully sending it (for a workflow that keeps a local draft copy until the send is confirmed) chains naturally through the completion events:
// Assumes a private field: FDraftItemID: string;
procedure TForm1.SendDraftThenDelete(ADraft: TTMSFNCCloudMicrosoftOutlookMailItem);
begin
FDraftItemID := ADraft.ID;
TMSFNCCloudMicrosoftOutlookMail1.OnSendMessage := DraftSentThenDeleted;
TMSFNCCloudMicrosoftOutlookMail1.SendMessage(ADraft);
end;
procedure TForm1.DraftSentThenDeleted(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
TMSFNCCloudMicrosoftOutlookMail1.DeleteMessage(FDraftItemID);
end;
Common mistakes
- Passing recipients as a plain string.
ARecipients(and the Cc/Bcc lists) areTStringListinstances of e-mail addresses, not a single delimited string. - Sending as an application without the right Graph permission. Sending
on behalf of another mailbox requires an application permission
(
Mail.Sendwith admin consent) in addition to the delegated permission used for interactive sign-in. - Deleting by a stale identifier. Delete the item (or ID) from a current listing; an ID from a much older listing may already point at a moved or deleted message.
Related API
TTMSFNCCloudMicrosoftOutlookMail—SendMessage,SendMailMessage,DeleteMessage,OnSendMessage,OnDeleteMessage