Send email from dynamics ax

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