Mohammed 的个人资料Dynamics AX日志留言簿网络 工具 帮助

日志


8月10日

Sending Emails from Dynamics AX without Outlook.

People often ask me if Outlook is necessary to send email from Dynamics AX, and my answer is.. Yes! ‘Out Of the Box’, Dynamics AX does need Outlook installed on the same machine as the AX client to send out emails.

However Dynamics AX can very easily be customized to send out standard reports via email, without outlook. We are talking about a really small modification here, there is only one method that needs to be edited, hence for clients that find outlook to be too expensive to fit their budget or if there are architectural issues (for example, installing outlook on Terminal Server),this code could be a life saver...

Note: AX – Outlook integration is confined to email, there are many other benefits of using outlook with dynamics ax, such as synchronization of crm contacts, transferring appointment to outlook calendar, etc. If the client chooses not to install outlook with Dynamics AX, then the company would be loose out on all these features.

Anyways.. this is what you need to do:

The following method is used to send emails from Dynamics AX:

Class\Info\Method\ReportSendMail

Out of the box, AX uses the class SysInetMail, which invokes the user email profile to find the outlook / email client that is being used on the system, obviously if it doesn’t find out it would throw an error.

There are 2 (possibly more, but 2 that I know of) other ways to send out mails from ax (easily).

1. Microsoft.CDO Com Object – Collaboration Data Objects can be used to send email and it comes pre installed on all AX supported versions of Windows.. as a matter of fact the emails generated by Alerts use the class SysMailer, which in turn uses a CDO object.

2. System.Net.Mail Assembly – In this example I choose to this path, simply because of the flexibility of the API.. Note: for this to work System.Net should be a part of the Ax References (AOT > References) ...by default it always is one of AX references, but there is no harm is double checking.

Here is the code... Note: in order to use this code one needs to agree to these Terms.

//<CHANGE>

// <DATE>##/##/###</DATE>

// <TRACKING_ID>Mohammed</TRACKING_ID>

// <DESCRIPTION>

// Email out of ax withot outlook.

// the default ax code uses inet to send email and the sysInetMail class check the users profiles for a outlook account.

// changing code to use System.Net.Mail assembly

// </DESCRIPTION>

//</CHANGE>

void reportSendMail(PrintJobSettings p1)

{

//Mohammed : Start Declaration

//SysINetMail m = new SysINetMail(); // Mo : Commented out old AX code

System.Net.Mail.MailMessage mailMessage;

System.Net.Mail.Attachment attachment;

System.Net.Mail.AttachmentCollection attachementCollection;

System.Net.Mail.SmtpClient myMail;

System.Net.Mail.MailAddress mailFrom;

System.Net.Mail.MailAddress mailTo;

str userMailAddress;

str receiverMailAddress;

str mailBody;

str smtpServer;

fileNameOpen fileNameForEmail;

str mail;

FileIOPermission perm;

userinfo userInfo;

//end Declaration

str fileName = 'axaptareport';

;

if (p1.format() == PrintFormat::ASCII)

fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'TXT'; // Mo : NL

//fileName = fileName + '.txt'; // Mo Commented this line

else if (p1.format() == PrintFormat::RTF)

fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'RTF';

//fileName = fileName + '.rtf';

else if (p1.format() == PrintFormat::HTML)

fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'HTM';

//fileName = fileName + '.htm';

//else if (p1.format() == PrintFormat::PDF) // Mohammed :Performance Testing : commentign this line and replacing the line below.

else if (p1.format() == PrintFormat::PDF || p1.format() == PrintFormat::PDF_EMBED_FONTS)// Mohammed :Performance Testing :(replacing the above line) addign this line as it was present in the jsRemotecontroller project.. can be removedd later..

fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999)+'PDF';

//fileName = fileName + '.pdf';

//Mohammed : Start Logic

mail = subStr(fileNameforEmail,(strlen(fileNameforEmail)-8),9);

select firstonly name from userInfo where userInfo.id == SysuserInfo::find().Id; // to find the user name

fileNameforEmail = winApi::getTempPath() + mail; // store attachment in a temp location

perm = new FileIOPermission(fileNameforEmail,'w');

if(!perm)

{

throw error("Cannot move attachment to temp location.");

return;

}

try

{

perm.assert();

}

catch

{

throw error("Cannot gain access to Temp location.");

return;

}

userMailAddress = SysUserInfo::find().Email; // find current users email address setup up in user //options

receiverMailAddress = p1.mailTo();

mailFrom = new System.Net.Mail.MailAddress(userMailAddress,userInfo.name);

mailTo = new System.Net.Mail.MailAddress(receiverMailAddress,"");

mailBody = "Email sent from " + CompanyInfo::name() + ", using Dynamics AX";

smtpServer = SysEmaiLParameters::find(false).SMTPRelayServerName;// using the SMTP server ip //setup in email Parameters

mailMessage = new System.Net.Mail.MailMessage(mailFrom,mailTo);

mailmessage.set_Subject(p1.mailSubject());

mailmessage.set_Body(mailBody);

//move attachment file to Temp folder

winapi::moveFile(p1.fileName(), fileNameforEmail);

attachementCollection = mailMessage.get_Attachments();

attachment = new System.Net.Mail.Attachment(fileNameforEmail);

attachementCollection.Add(attachment);

myMail = new System.Net.Mail.SmtpClient(smtpServer);

mymail.Send(mailmessage);

winApi::deleteFile(fileNameforEmail); // delete temp file

CodeAccessPermission::revertAssert();

// Mohammed end

}

I hope this code is of use to you.

Thanks for reading..

 

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://dynamic-ax.spaces.live.com/blog/cns!13619E6948204DE3!320.trak
引用此项的网络日志