Thursday, March 31, 2022

D365 F&O send email through x++ code

 

1- We need first to check  System Administration >> Setup >> Email parameters 




2- Create below Method to use in your Class  

SendMail::SendMail("New approve required" 
                , strFmt("New  approval required  %1" , _TransID )
                , strFmt("Body  %1" , strBody), curUserId());

class SendMail
{

public static void SendMail(str strsenderName , str subject, str strbody, UserId userId )//= "sheriffayed")

    {

        SysOutgoingEmailTable outgoingEmailTable;

        SysEmailItemId nextEmailItemId;

        Map map;

        str SenderName, SenderEmail, To , Body;

    ;

        SenderName = strsenderName;

    SenderEmail = "admin@xxx.com";

    To = SysUserInfo::find(userId).Email;

    Subject = subject;

    Body = strbody ; // "<B>Test Body of the email</B>";

    nextEmailItemId = EventInbox::nextEventId();

    outgoingEmailTable.EmailItemId = nextEmailItemId;

    outgoingEmailTable.IsSystemEmail = NoYes::No;

    outgoingEmailTable.Sender = SenderEmail;

    outgoingEmailTable.SenderName = SenderName;

    outgoingEmailTable.Recipient = To;

    outgoingEmailTable.Subject = SysEmailMessage::stringExpand(Subject, map);

    outgoingEmailTable.Priority = eMailPriority::Normal ;

    outgoingEmailTable.WithRetries = false;

    outgoingEmailTable.RetryNum = 0;

    outgoingEmailTable.UserId = userId;

    outgoingEmailTable.Status = SysEmailStatus::Unsent;

    outgoingEmailTable.Message = Body;

    outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();

    outgoingEmailTable.insert();

     }

    }

No comments:

Post a Comment