Dynamics AX
  RSS Feed  LinkedIn  Twitter
Want to turn you're data into a true asset? Ready to break free from the report factory?
Ready to gain true insights that are action focused for truly data informed decisions?
Want to do all of this across mutliple companies, instances of Dynamics and your other investments?
Hillstar Business Intelligence is the answer then! (www.HillstarBI.com)

Hillstar Business Intelligence for Microsoft Dynamics AX and NAV on Mobile, Desktop, Tablet


Let us prove to you how we can take the complexity out of the schema and truly enable users to answer the needed questions to run your business! Visit Hillstar Business Solutions at: www.HillstarBI.com

Wednesday, November 30, 2005

SysMailer in Dynamics AX

Something that we have found a good bit useful in our custom development of AX is the SysMailer class. With this class, you can - generate and send emails. To give an example of how we used this, we added a new enum value to the Sales Status of a sales order called 'Credit Hold'. When a customer is past due, or is over there given credit limit, then the sales order is placed on 'Credit Hold' and a email is sent out to a given list of people (alias group of emails contained on the email server). The people that get this list decided when the sales order can be actually turned from Credit Hold, to Open Order. The automatically generated email is what keeps the flow of this information system controlled, and not being dependant on a user to have to send a email out.

Without getting further into the benefits of this, or the example I gave you, let's just dive right in and look at the SysMailer example code:


SysMailer mail;
;

mail = new SysMailer();
mail.SMTPRelayServers().add("mail.yoursmtpserver.com");
mail.fromAddress("test@test.com");
mail.tos().add("you@you.com");

// Build the Message
mail.htmlBody(strfmt(""));
mail.subject(strfmt(""));
mail.sendMail();


That's it! Pretty straight forward, and great piece of code that can be very useful when alerting, and sharing of information and events as they occur. I am sure you can see how this can be used, in fact, I believe that Axapta Alerts! product for keeping you informed about what all is going on within your Ax environment use's similar code a lot.

Find a job at: www.DynamicsAXJobs.com

11 Comments:

Anonymous S.C.GAN said...

I try to use the example it wasn't working. Finally i found it is error in:

s = new Statement();

so it is working when i change to:

s = cn.createStatement();

Thanks for your sample :)

8:14 PM  
Anonymous Jules said...

Hi there,I'm a newbie at this.
I've tried to implement that code,and I'm wondering if I'm doing right.Let me expose the code:
void mailexistencias()
{
sysmailer mail;
inventsum inventsum;
str c;
while select * from inventsum
where !inventsum.PhysicalInvent
c = inventsum.ItemId;

if (!inventsum.PhysicalInvent)
{mail = new SysMailer();
mail.SMTPRelayServers().add("192.168.168.76");
mail.fromAddress("tes@textileseuropeos.com");
mail.tos().add("mymail@mymail.com");

// Builds message
mail.body(c);
mail.subject(strfmt("No hay existencias"));
mail.sendMail(); }

}
Thank you anyway.Hope this site,had more activity soon,not much posting in here from axapta programers.

5:56 AM  
Blogger brandon said...

jules,

If you would have left your email address I might have been able to help you out. Anyway, your code seems to look right, without me have to test it out. Where is the code failing?

7:17 AM  
Anonymous Jules said...

Thank you brandon to have pitty of
a newbie like me :).
My e-mail is JamasXC@msn.com.
Well,perhaps the code isn't the fail.I've paste that code in a table method named inventsum.
the purpose is to send and e-mail of the itemid's when existences get to "0" value,(line: if (!inventsum.physicalinvent)).

I have some knowledge of Java,C+ and POO languages,but I just don't know where to place some codes in axapta to do things and what is worst,I haven't found good guides,manuals or training courses yet in Spain.

9:49 AM  
Anonymous Malcolm Burtt said...

Its also worthwhile using name servers with SysMailer as this will enable you to validate email addresses. SysMailer has a validateAddress() method for this purpose but needs access to a local DNS to do the job with any speed. I override the validateAddress method as shown below to ensure that SysMailer has a DNS it can use....


public int validateAddress(str address)
{
int ret;
COM dnsServers;

// Has DNS server been set up?
if (SysEmailParameters::find().DNSServerName)
{
dnsServers = _com.DNSServers();

if ( ! dnsServers.count() )
{
dnsServers.add(SysEmailParameters::find().DNSServerName);
}
}

return super(address);
}


Note that my code references SysMailer's underlying COM object directly rather than using the SysMailer.DNSServers() wrapper method. This is because I've found that the SysMailer.DNSServers().add() approach causes a crash.

Its also worth noting that SysMailer lets you build beautifully formated HTML emails (via sysMailer.htmlBody()). If you do this make sure you also include a plain text body. To make building the HTML body easier consider building a bunch of methods that return formated HTML tags. e.g.


static str table(str 200 attr = 'Border=0 CellSpacing=0 CellPadding=0')
{
;
return '<table ' + attr + '>';
}

static str tableEnd()
{
;
return '</table>';
}

static str header1(TempStr t)
{
;
return '<h1>'+t+'</h1>';
}


That way you can just call these classes and not have to remember the syntax of the HTML every time.

9:20 AM  
Blogger meady said...

Hi Brandon, I hope this comment gets to you even though its nearly 4 years on since this post! I'm currently using Sysmailer but I have found that when passing and invalid email address like "Mr Bloggs" either our mail server or sysMailer resolves this address to be several seemingly random addresses and emails those! can you shed any light on this behaviour? any help would be most appreciated .. my email address is wameade@gmail.com .. kind regards, Christian ...

2:31 AM  
Blogger Anisha said...

Hi,

Does "straight out of the box" emailing work in AX2009?

I have updated the email parameters form and started the email batch processor however my emails sit in the outbox of Outlook. Do you know why this is so?

AX has a built in email client if a email adress is inserted for a contact and selecting the email icon, how can I specify more than one contact person and enable the cc, bcc fields?

2:26 PM  
Blogger Mohamed Amine HAMDAOUI said...

Hi,

Is it possible to parameter an "acknowledgment the receipt", so when the recipient get the message, the sender will be notified?

Thanks in advance,
Mohamed Amine

8:35 AM  
Anonymous Anonymous said...

Thanks!!!!
But in Dynamics Ax 2009 is diferent code:
SysMailer mail;
;

mail = new SysMailer();
mail.SMTPRelayServer("smtp.test.com",01,"test@test.com","test");//smtp,port,email,password
mail.fromAddress("test@test.com");
mail.tos().appendAddress("test@test.com");
// Build the Message
mail.htmlBody(strfmt("test"));
mail.subject(strfmt("test"));
mail.sendMail();

5:42 AM  
Anonymous Judy said...

Of all these codes, where can I place it? Should I make a new Class or can I use the available SysMailer Class? Helpless..Thanks. need help on auto send email from Axapta. Currently, I am using AX 3.0 SP4.tataruku@yahoo.com,Please. Thanks.

11:04 PM  
Blogger Dilip said...

Hey mate,

Thanks for the code snippet, I just leveraged this in my 2012 email testing exercise.

http://daxdilip.blogspot.com/2011/08/sysmailer-api-send-email-through-code.html

Cheers.
Dilip

1:18 AM  

Post a Comment

Links to this post:

Create a Link

<< Home


Copyright 2005-2011, J. Brandon George - All rights Reserved