Monday, May 23, 2022

How to send an email in D365FO using x++

 In D365FO requirements most of them need to send  the email. So for that we can have multiple ways. Please check the below code.


Simple Way:

public void sendEmail() { var builder = new SysMailerMessageBuilder(); builder.addTo("To email"); builder.setFrom("sender email"); builder.setSubject("Subject"); builder.setBody("Body"); var message = builder.getMessage(); SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message); }


With template:

public void sendEmail() { SysEmailTable sysEmailTable = SysEmailTable::find('TestEmail');//Get from template. SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(
                            sysEmailTable.EmailId, sysEmailTable.DefaultLanguage); var messageBuilder = new SysMailerMessageBuilder(); messageBuilder.addTo("To email"); messageBuilder.setFrom(sysEmailTable.SenderAddr);//, sysEmailTable.SenderName); messageBuilder.setSubject(sysEmailMessageTable.Subject); messageBuilder.setBody(sysEmailMessageTable.Mail); var message = builder.getMessage(); //SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message); SysIMailer mailer = sysMailers.lookup('SMTP'); if (mailer is SysIMailerNonInteractive) { SysIMailerNonInteractive nonInteractiveMailer = mailer; nonInteractiveMailer.sendNonInteractive(messageBuilder.getMessage()); } else if (mailer is SysIMailerInteractive) { SysIMailerInteractive interactiveMailer = mailer; interactiveMailer.sendInteractive(messageBuilder.getMessage()); } }

--------------------send mail to multiple users-------------

private void sendEmail() { SysEmailSystemTable sysEmailTable; SysEmailMessagesystemTable emailMessageTable; var user = xUserInfo::find(); Map sysMailers = SysMailerFactory::getMailers(); container conCCEmails, conToEmails; MyTable myTable; sysEmailTable = SysEmailSystemTable::find('TestEmail');//Get from template. select firstonly emailMessageTable where emailMessageTable.EmailId == sysEmailTable.EmailId; //emailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, // sysEmailTable.DefaultLanguage); select * from myTable; conCCEmails = str2con(myTable.CCEmailFR,';');// adding emails in CC; ConToEmails = str2con(myTable.ToEmail,';');// To emails. var messageBuilder = new SysMailerMessageBuilder(); for (int i=1; i<=conLen(conCCEmails); i++) { messageBuilder.addCc(conPeek(conCCEmails, i)); } for (int i=1; i<=conLen(ConToEmails); i++) { messageBuilder.addTo(conPeek(ConToEmails, i)); } str curDate = date2Str(today(), 321, DateDay::Digits2, DateSeparator::Hyphen, DateMonth::Digits2, DateSeparator::Hyphen, DateYear::Digits4 ); // We can use below one also /*var messageBuilder = new SysMailerMessageBuilder(); messageBuilder.addTo(myTable.ToEmail) .setSubject(emailMessageTable.Subject) .setBody(emailMessageTable.Mail);*/ //get subject from template messageBuilder.setSubject(emailMessageTable.Subject + '-' + curDate); messageBuilder.setBody(emailMessageTable.Mail);//get body from template messageBuilder.setFrom(sysEmailTable.SenderAddr);// Sender email var message = messageBuilder.getMessage(); messageBuilder.addAttachment(attachmentStream,fileName);//Attach file // We can use below one also //SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message); SysIMailer mailer; SysMailerSMTP smtp = new SysMailerSMTP(); mailer = sysMailers.lookup('SMTP'); if (mailer is SysIMailerNonInteractive) { SysIMailerNonInteractive nonInteractiveMailer = mailer; nonInteractiveMailer.sendNonInteractive(messageBuilder.getMessage()); } else if (mailer is SysIMailerInteractive) { SysIMailerInteractive interactiveMailer = mailer; interactiveMailer.sendInteractive(messageBuilder.getMessage()); } }

Using Email processing table:


public void sendEmail() { SysEmailTable sysEmailTable = SysEmailTable::find('TestEmail'); SysEmailMessageTable sysEmailMessageTable; SysOutgoingEmailTable sysoutgoingEmailTable; //SysOutgoingEmailData outgoingEmailData; sysEmailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage); sysoutgoingEmailTable.EmailItemId = EventInbox::nextEventId(); sysoutgoingEmailTable.TemplateId = sysEmailTable.EmailId; sysoutgoingEmailTable.Sender = sysEmailTable.SenderAddr; sysoutgoingEmailTable.SenderName = sysEmailTable.SenderName; sysoutgoingEmailTable.Recipient = 'receiver@test.com'; sysoutgoingEmailTable.Subject = sysEmailMessageTable.Subject; sysoutgoingEmailTable.Message = sysEmailMessageTable.Mail; sysoutgoingEmailTable.Priority = sysEmailTable.Priority; sysoutgoingEmailTable.WithRetries = true; sysoutgoingEmailTable.RetryNum = 0; sysoutgoingEmailTable.UserId = curuserid(); sysoutgoingEmailTable.Status = SysEmailStatus::Unsent; sysoutgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime(); sysoutgoingEmailTable.insert(); }

Need to run below job.

System administration>Periodic tasks>Email processing>Email distributor batch.

With placeholder:

public void sendEmail() { SysEmailTable sysEmailTable = SysEmailTable::find('TestEmail'); SysEmailMessageTable sysEmailMessageTable = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage); Map placeholder = new Map(Types::String, Types::String); placeholder.insert("Name", sysEmailTable.SenderName); placeholder.insert("Date", strFmt("%1", DateTimeUtil::getSystemDateTime())); str body = SysEmailMessage::stringExpand(sysEmailMessageTable.Mail, placeholder); str subject = SysEmailMessage::stringExpand(sysEmailMessageTable.Subject, placeholder); var builder = new SysMailerMessageBuilder(); builder.addTo("receiver@test.com"); builder.setFrom(sysEmailTable.SenderAddr);

builder.setSubject(subject); builder.setBody(body); var message = builder.getMessage(); SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message); }


Keep daxing!!

No comments:

Post a Comment