Trending: Jawaan


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.

//

// ##/##/###

// Mohammed

//

// 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

//

//

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..


View the original article here

1 Responses to “Sending Emails from Dynamics AX without Outlook.”

Anonymous said...
July 11, 2011 at 7:45 AM

Users would like to use the cc: (as well as to:). There doesn't seem to be a provision for cc: or Bcc: included in this posted solution.


About Me