Tags
Automation, AX, AX 2012, AX Email, Build Scripts, Dynamics AX, PowerShell, Setup
When looking at automating my AX 2012 processes, the first thing I was interested in was the ability to send emails when steps were completed. This would allow me to kick off my process and walk away, tracking it on my phone if necessary.
The function I created for this is Send-Email and can be found on Codeplex. It takes 7 parameters:
- SMTPServer (SMTP server that will send the email)
- From (Who the email is from)
- To (Who the email is to)
- Subject (Email subject)
- Body (Email body)
- Priority (Email priority)
- FileLocation (Allows you to attach a file if used. I primarily use this to attach my log files)
This function uses the Net.Mail.MailMessage and Net.Mail.SmtpClient to send an email message. I usually default some of these parameters in the scripts I use to call these functions but we’ll get into that when I walk through my build function. You can view the code in this function using PowerShell ISE.
The way that I currently test and run these standalone PowerShell functions is to use my profile. In my profile, I include a link to a PowerShell script that holds my function locations. I do this so that when I add new functions, I can just include them in my D2D_PSFunctions.ps1 script and they will be automatically included by my profile the next time I open up PowerShell. You can get more info on what I’m doing in an earlier post as well as Codeplex.
Please take a look at this function to get an idea of what is going on. There may be optimizations or better ways to do things in PowerShell but I was teaching myself PowerShell while I wrote all of this.
Pingback: D2DDynamics custom PowerShell module | Day 2 Day Dynamics
Can the current Send-Email function handle sending to multiple recipients?
I am using the xyz_variables.ps1 file to configure SMTP as follows:
$SMTPServer = ‘mail.mycompany.com’
$MailMsg = New-Object Net.Mail.MailMessage
$MailMsg.From = “buildmaster@mycompany.com”
$MailMsg.To.Add(“me@mycompany.com”)
$MailMsg.To.Add(“you@mycompany.com”)
$MailMsg.Subject = ‘Default subject’ #This is generally set in each function but I have a default here to stop email failures
$MailMsg.Body = ‘Default body’ #This is generally set in each function but I have a default here to stop email failures
#$MailMsg.Priority = ‘Normal’ #This isn’t really necessary as Normal is the default value for Net.Mail.MailMessage. Should be used if you want to change it.
When I use the following format to call the Send-Email (from Function_Build-AXModel.ps1, I only receive an email to “you@mycompany.com”… the last one in the list of To.Add() calls…
Send-Email -SMTPServer $SMTPServer -From $MailMsg.From -To $MailMsg.To -Subject $MailMsg.Subject -Body $MailMsg.Body -Priority $MailMsg.Priority
Thanks!
-Thomas
LikeLiked by 1 person
Thomas, thank you for the question. This appears to be a bug in the Send-Email function regarding the parameter type of string passed in and how it is read from the parameter when it is adding it back to the System.Net.Mail.MailAddressCollection. I will have a fix for this in the next build of the module. A temporary workaround would be to use an email distribution list in Exchange. https://technet.microsoft.com/en-us/library/cc164331(v=exchg.65).aspx
LikeLike
I made the following change to Send-Email function to handle multiple recipients via multiple $MailMsg.To.Add() calls for configuration…
#Email structure
$Msg.From = $From
#$Msg.To.Add($To) #TLW comment in lieu of below
$Msg.subject = $Subject
$Msg.body = $Body
$Msg.Priority = $Priority
# TLW update to handle multiple recipients
$addresses = $To.split(” “)
foreach( $address in $addresses )
{
$Msg.To.Add($address)
}
# TLW endMod
LikeLiked by 1 person
Thanks for the info. It is very similar to the changes that I have made as well. The only real difference is that I’ve changed the To input parameter from String to System.Net.Mail.MailAddressCollection. You can then loop through the collection without parsing the string. This would make using the method a little more complicated outside of my functions but all of the functions in my module that currently call Send-Email, already pass this object.
LikeLike