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); }
}