Skip to main content
Business Central forum
Suggested answer

AzureAD App Send Email

editSubscribe (0) ShareShare
ReportReport
Posted on by

Hello,

We're developing an app that is accessing the BC API using an AzureAD registered app. We're successfully sending POST and PATCH requests to Business Central, but we're working on exception handling and trying to - as a first step - get the app to trigger an email send.

We've written a Codeunit that is triggered when our app changes a field in one of the tables. The Codeunit runs successfully, reads the data from our staging table and moves it into the final data table. We've added a catch on the final Insert() operation so that if it fails it runs our EmailNotify procedure, which is defined in the same codeunit.

We've debugged this and it runs. It's not sending the email, though as it's trying the send as our app. 

pastedimage1652869383820v1.png

What I want to know is do we need to assign the app permissions at the AzureAD side of things, and if that's the case what permissions are required? If it's an issue at the Business Central side of things, what permissions do we need to assign our AzureAD application to allow it to send the email?

I'm absolutely certain that other devs will have apps that do this, and I've got the email sending working in one of our other extensions. The only difference is this is coming from an AzureAD registered app, even though the app itself isn't doing the sending, it's just triggering the codeunit.

Any help would be appreciated. We're stumbling around in the dark on this.

Categories:
  • RE: AzureAD App Send Email

    local procedure AlertNotify(header: Text[50]; message: Text[200])
        var
            TxtRecipientList: List of [Text];
            EmailObj: Codeunit "Email";
            EmailMsg: Codeunit "Email Message";
        begin
            //DEBUG:
            TxtRecipientList.Add('testemailaddress@ourdomain.co.uk');
    
            EmailMsg.Create(
                TxtRecipientList,
                header,
                message,
                false
            );
            EmailObj.Send(EmailMsg, Enum::"Email Scenario"::Default);
            GetSender();
        end;

    This is our AlertNotify procedure. It was lifted from our other extension which is triggered by a job queue entry failure. It all works fine, and when a job fails we get an email.

    All we're trying to do is run this procedure as part of our codeunit, which is triggered by our app changing a field - the OnModify trigger detects a change and then executes our codeunit. As I say, the codeunit runs, it goes into the AlertNotify procedure and runs all the code - it's just not able to send the email and gives the above mentioned reason.

    We already have an account set up as the sending account, and this is all working fine in the other extension. The issue is the listed sender in this case is our AzureAD app, and this seems to be where the problem lies.

    pastedimage1652871336448v1.png

    The codeunit is being triggered by a change made by our automated integration service, but it's obviously passing through the OAuth credentials that it receives before POSTing the data. Interestingly, when I click on the failed message and select Show Source, it pops up a user card for one of our users.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,714 Super User on at
    RE: AzureAD App Send Email

    I am not sure that i understand exactly what you are trying to do without having access to your code.

    But if you want the app to trigger e-mails you must use an e-mail account that is not a personal account but is a generic a-mail account defined in Business Central.

    When i implement BC i have the personal e-mail accounts defined but also a generic default address like no-reply@mycompany.com

    So your app would need to use that e-mail account to send e-mail.

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,068 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,591 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans