Ax2009中使用CLR发送邮件

时间:2021-01-02 15:32:28

由于Ax2009系统方法SysMailer 发送中文的时候会乱码,一直找不到原因,用.NEt Framwork的类库可以解决中文乱码的问题。
static void CKT_DotNetMail(Args _args)
{
System.Net.Mail.SmtpClient smtp;
System.Net.NetworkCredential Credential;
System.Net.Mail.MailMessage message;
System.Net.Mail.MailAddress mFrom,mTo;

// SysMailer mailer = new SysMailer();
InteropPermission _InteropPermission;

SysEmailParameters parameters ;
str username ,password;
;

_InteropPermission = new InteropPermission(InteropKind::ComInterop);
_InteropPermission.assert();

/*
mailer.SMTPRelayServer(parameters.SMTPRelayServerName
,parameters.SMTPPortNumber
,parameters.SMTPUserName
,SysEmailParameters::password()
,parameters.NTLM);
*/
parameters = SysEmailParameters::find();
username = "ABC@sina.com";
password = "ABCD;

smtp = new System.Net.Mail.SmtpClient(parameters.SMTPRelayServerName);
Credential = new System.Net.NetworkCredential(username,password);
smtp.set_Credentials(Credential);

mFrom = new System.Net.Mail.MailAddress("ABC@sina.com");
mTo = new System.Net.Mail.MailAddress("ABC@sina.com");

message = new System.Net.Mail.MailMessage(mFrom,mTo);
// message.set_From(mFrom);
message.set_Subject("YYYYYYY");
message.set_Body("看得见疯狂的");

message.set_BodyEncoding(System.Text.Encoding::get_UTF8());

smtp.Send(message);

info("Finish");
CodeAccessPermission::revertAssert();
}