当前位置: 首页 > news >正文

D365 使用电子邮件模板在 X++ 中发送电子邮件

文章目录

  • 前言
  • 一、设置模板
  • 二、代码
    • 示例1
    • 示例2


前言

参考1
参考2

一、设置模板

系统内路径
组织管理 > 设置 > 组织电子邮件模板
html模板,如果有字段变量可以在正文中用%%占位符,后续代码处理插入占位符值。
在这里插入图片描述

二、代码

示例1

代码如下(示例):

public void generateEmail()
{//电子邮件模板替换#define.SalesIdToken('salesid')SalesTable salesTable;CustTable custTable;SysEmailId emailId;Map templateTokens;str emailSenderName;str emailSenderAddr;str emailSubject;str emailBody;str emailToAddress;salesTable = SalesTable::find(salesId);custTable = CustTable::find(salesTable.CustAccount);emailId = 'TestId'//电子邮件IdtemplateTokens = new Map(Types::String, Types::String);templateTokens.insert(#SalesIdToken, salesTable.SalesId);if (emailId){[emailSubject, emailBody, emailSenderAddr, emailSenderName] =this.getEmailTemplate(emailId, custTable.languageId());}var messageBuilder = new SysMailerMessageBuilder();messageBuilder.addTo(emailToAddress).setSubject(emailSubject).setBody(SysEmailMessage::stringExpand(emailBody, templateTokens));//.setBody(SysEmailMessage::stringExpand(emailBody, SysEmailTable::htmlEncodeParameters(templateTokens)))//.addCc(emailCcAddress);DocuRef                         docuRef;DocuValue                       docuValue;DocuType                        docuType;while select docuRefwhere docuRef.RefCompanyId  == salesTable.DataAreaId &&docuRef.RefTableId      == tableNum(SalesTable) &&docuRef.RefRecId        == salesTable.RecId join docuValuewhere docuRef.ValueRecId    == docuValue.RecIdjoin docuTypewhere docuType.TypeId == docuRef.TypeId{if (docuRef){System.IO.MemoryStream          stream   = new System.IO.MemoryStream();DocumentManagement::getAttachmentStream(docuRef).CopyTo(stream);messageBuilder.addAttachment(stream, docuValue.FileName+"."+docuValue.FileType);   }}if (emailSenderAddr){messageBuilder.setFrom(emailSenderAddr, emailSenderName);                SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(messageBuilder.getMessage());}
}public container getEmailTemplate(SysEmailId _emailId, LanguageId _languageId)
{var messageTable = SysEmailMessageTable::find(_emailId, _languageId);var emailTable = SysEmailTable::find(_emailId);if (!messageTable && emailTable){messageTable = SysEmailMessageTable::find(_emailId, emailTable.DefaultLanguage);}if (messageTable){return [messageTable.Subject, messageTable.Mail, emailTable.SenderAddr, emailTable.SenderName];}else{warning("没有找到邮件模板!");return ['', '', emailTable.SenderAddr, emailTable.SenderName];}
}

示例2

代码如下(示例):

public void generateEmail()
{//电子邮件模板替换#define.SalesIdToken('salesid')System.Exception exception;SysEmailParameters sysEmailParameters;SalesTable salesTable;CustTable custTable;SysEmailId emailId;Map templateTokens;str emailSenderName;str emailSenderAddr;str emailSubject;str emailBody;str emailToAddress;str emailCcAddress;try{sysEmailParameters  = SysEmailParameters::find();salesTable = SalesTable::find(salesId);custTable = CustTable::find(salesTable.CustAccount);emailId = 'TestId'//电子邮件IdtemplateTokens = new Map(Types::String, Types::String);templateTokens.insert(#SalesIdToken, salesTable.SalesId);if (emailId){[emailSubject, emailBody, emailSenderAddr, emailSenderName] =this.getEmailTemplate(emailId, custTable.languageId());}//邮件信息System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(emailSenderAddr,emailToAddress);System.Net.Mail.MailAddressCollection mailCCCollection = mailMessage.get_CC();mailCCCollection.Add(emailCcAddress);while select docuRefwhere docuRef.RefCompanyId  == salesTable.DataAreaId &&docuRef.RefTableId      == tableNum(SalesTable) &&docuRef.RefRecId        == salesTable.RecId join docuValuewhere docuRef.ValueRecId    == docuValue.RecIdjoin docuTypewhere docuType.TypeId == docuRef.TypeId{if (docuRef){System.IO.MemoryStream          stream   = new System.IO.MemoryStream();stream.Position = 0;//需要把流的位置设置到开始,不然后续获取不到mailmessage.get_Attachments().Add(new System.Net.Mail.Attachment(stream, docuValue.FileName+"."+docuValue.FileType));}}mailmessage.set_Subject(emailSubject);mailmessage.set_IsBodyHtml(true);mailmessage.set_Body(SysEmailMessage::stringExpand(emailBody, SysEmailTable::htmlEncodeParameters(templateTokens)));//smtp设置System.Net.Mail.SmtpClient myMail = new System.Net.Mail.SmtpClient(sysEmailParameters.SMTPRelayServerName, sysEmailParameters.SMTPPortNumber);myMail.set_EnableSsl(sysEmailParameters.SMTPRequireSSL);mymail.set_Credentials(New System.Net.NetworkCredential(sysEmailParameters.SMTPUserName, sysEmailParameters.password()));mymail.Send(mailMessage);mailMessage.Dispose();}catch(Exception::CLRError){exception = ClrInterop::getLastException();while(exception.get_InnerException() != null){exception = exception.get_InnerException();}error(exception.get_Message());}catch (exception){error(exception.Message);     }
}


http://www.mrgr.cn/news/68054.html

相关文章:

  • unity游戏开发之--人物打怪爆材料--拾进背包的实现思路
  • 【深度学习滑坡制图|论文解读3】基于融合CNN-Transformer网络和深度迁移学习的遥感影像滑坡制图方法
  • 如何写研究的结论与讨论部分
  • 2024软件测试面试热点问题
  • ubuntu 22.04 server 安装 和 初始化 LTS
  • FPAGA学习~问题记录
  • 【UE5】Cesium GlobePawn 如何Fly To
  • uniapp中使用原生ajax上传文件并携带其他数据,实时展示上传进度
  • 备考25年二建,最好这样选专业!
  • nacos注册中心简介
  • vue2与vue3的全局通信插件,如何实现自定义的插件
  • 杨辉三角,洗牌算法
  • 探索C语言数据类型
  • 前端基础-html-注册界面
  • [Docker#1] 专栏前言 | 亿级高并发架构演进之路
  • 【大数据学习 | kafka高级部分】kafka的数据同步和数据均衡
  • 经典网络模型
  • brainpy 动力学编程基础
  • 最新kubernetes搭建(k8s)(已成功搭建)
  • 【科普】卷积、卷积核、池化、激活函数、全连接分别是什么?有什么用?
  • 【软考】错题总结:1106
  • [CUDA] 判断一个指针是GPU还是CPU
  • 远程控制项目第四天 功能实现
  • Claude Prompt:博弈天平|缘起自李继刚老师的提示词
  • 使用PyQt5设计一个简易计算器
  • 【测试】【Debug】pytest运行后print没有输出