In this article we are showing how to read XML file using PowerShell, this is a sample example based on this we can develop the PowerShell scripts with config XML file, without touching the code modify the XML config file and send the mail based on the config file requirement, maintain the XML file will be very good and easy.
Here is the sample XML file to read the PowerShell for sending mail with to and from address with Subject and SMTP server information.
Here is the PowerShell code reading the XML file and sending Mail.
[xml]$xmlfile = Get-Content D:\Reading.xml
$EmailFrom = $xmlfile.mail.mailFrom
$EmailTo = $xmlfile.mail.mailTo
$EmailSubject = $xmlfile.mail.mailSubject
$SMTPServer = $xmlfile.mail.SMTPRelay
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($EmailFrom)
$mailmessage.Subject = $EmailSubject
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer)