Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Send email using email message

Posted on by 55
Hi All,
 
I am trying new SMTP functionality to send emails through my AL code but it is not working. I am not sure if I am missing something. Below is my sample code:-
 
page 60181 HelloWorldExt
{
ApplicationArea = All;
Caption = 'Test Email';
UsageCategory = tasks;
trigger OnOpenPage();
begin
SendEmail();
Message('Message Send');
end;
procedure SendEmail()
var
TxtDefaultCCMailList: List of [Text];
TxtDefaultBCCMailList: List of [Text];
TxtRecipientsList: List of [Text];
begin
EmailAccount.Reset();
EmailAccount.SetFilter(Connector, 'SMTP');
if EmailAccount.FindLast() then;
TxtRecipientsList.Add('xxxx@xxxx.com');
EmaiMsg.Create(
TxtRecipientsList,
'Subject - Test Email',
'Body - This is Email Body',
false
);
EmailObj.Send(EmaiMsg, Enum::"Email Scenario"::Default);
end;
Var
EmailObj: Codeunit "Email";
EmaiMsg: Codeunit "Email Message";
EmailAccount: Record "Email Account";
}
 
Thanks,
Amit
Categories:
  • T_Mauser Profile Picture
    T_Mauser 397 User Group Leader on at
    RE: Send email using email message

    I also had that problem in the beginning - solved it with adding the necessary permissions to the mail boxes.

    You can find it in the exchange admin tool:

    Mailboxes -> Mailbox Permissions ->Manage mailbox delegate -> Send as

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Send email using email message

    Not an uncommon error. I think smtp connection by default is off on Office365 mail accounts. So they need to be enabled before you can use them like SMTP accounts in Business Central. We are here if you need further assistance.

  • Amit Bansal Profile Picture
    Amit Bansal 55 on at
    RE: Send email using email message

    Thanks, Inge for quick reply. I checked "Email Outbox" and there it is giving me "Cannot authenticate the credentials on server smtp.office365.com." error. I will work with admin and correct this issue and will come back if it is still not resolved.

    Thanks,

    Amit.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Send email using email message

    The way you have organized your code makes it necessary to define it because you have used it in your code.

    You can send the message with out using a scenario but just refer to the email account instead.

    Send(EmailMessage: Codeunit "Email Message"; EmailAccount: Record "Email Account" temporary): Boolean
    Emailobj.Send(EmailMsg;EmailAccount);
    Should work for you.
    But i think you should define your EmailAccount variable as temporary because that is compliant with the send methods signature.
  • Amit Bansal Profile Picture
    Amit Bansal 55 on at
    RE: Send email using email message

    Hi Inge,

    I didn't defined default scenario. I am not able to understand why we have to define default scenario.

    Is it required.

    Thanks,

    Amit

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Send email using email message

    Just by looking at your code it looks ok. But i have not tried to run it.

    Just som things to check.

    Have you defined a default scenario?

    Can you see the email you try to send in the outgoing mail queue and see what status it got there?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans