Send email from dynamics ax

本文介绍了三种使用C#发送邮件的方法:第一种是使用SysMailer类直接发送邮件;第二种是通过标准邮件机制内置在系统中发送邮件,并保存邮件发送状态;第三种是通过邮件模板定义发送邮件。每种方法都有其适用场景和限制。

method1----------------------------------------------------

static void jay_testmail(Args _args)
{
SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject,body;
InteropPermission interopPermission;
SysMailer mailer;
System.Exception e;

;
if (parameters.SMTPRelayServerName)
{
relayServer = parameters.SMTPRelayServerName;
}
else
{
relayServer = parameters.SMTPServerIPAddress;
}

portNumber = parameters.SMTPPortNumber;
userName = parameters.SMTPUserName;
password = SysEmailParameters::password();
subject = "Subject line for the email";
body = "<B>Body of the email</B>";

CodeAccessPermission::revertAssert();

try
{
interopPermission = new InteropPermission(InteropKind::ComInterop);
interopPermission.assert();

mailer = new SysMailer();
mailer.SMTPRelayServer(relayServer,portNumber,userName,password, parameters.NTLM);
mailer.fromAddress("xiangliqi@qq.com");
mailer.tos().appendAddress("jiaqiang@tps-logistics.com");
mailer.subject(subject);
mailer.htmlBody(body);
mailer.sendMail();
CodeAccessPermission::revertAssert();
info("Email has been send!");
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
info ("Failed to Send Email some Error occure");
}

}

 

 

 

method2-------------------------------------------------------

The most recommended way is to use the standard mail mechanism built in the system. 
That way copies of any email you send will be save in the Email sending status form (based on tableSysOutgoingEmailTable) and you will be able to monitor and see status of sent emails: 

This mechanism based on a system table that contain the emails, and a batch job that scan that table and send the emails, one by one (with retries and status controlling). 
This technique is very simple to use and therefore it has some disadvantages; you cannot add attachments or use advanced email properties (cc, bcc, priority flag, etc). 
To use this mechanism, first you have to make sure you a SMTP server configured and running. 
Go to
  Administration -> Setup -> E-mail parameters 
and fill the required settings

Next step is to make sure the E-mail distributor batch is up and running. 
Go to
  Basic -> Inquiries -> Batch Job and check if the batch job exists with status ExecutingorWaiting

 

static void testmail2(Args _args)
{
SysMailer mail;
SysOutgoingEmailTable outgoingEmailTable;
SysEmailItemId nextEmailItemId;
Map map;
str SenderName, SenderEmail, To, Subject, Body;
;

SenderName = "tps jiaqiang";
SenderEmail = "xiangliqi@qq.com";
To = "jiaqiang@tps-logistics.com";
Subject = "Subject line for the email";
Body = "<B>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 = curUserId();
outgoingEmailTable.Status = SysEmailStatus::Unsent;
outgoingEmailTable.Message = Body;
outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();
outgoingEmailTable.insert();

info("xixi");
}

 

method3--------------------------------------------------------------------

通过AX邮件模板的定义发送邮件

static void jay_testmail3(Args _args)
{
str emailId = "JayEmail";
str language = "zh-hans";
str address = "jiaqiang@tps-logistics.com";
Map mappings = new Map(Types::String, Types::String);
;
mappings.insert("vendor", "lenovo");

SysEmailTable::sendMail(emailId, language, address, mappings, "", "", true, "jiaqiang");
info("xixi");


}

 

转载于:https://www.cnblogs.com/xiangliqi/p/4815967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值