How to send Email using command in Windows

You can not use Command Prompt directly to send an email, but using PowerShell allows you to use the underlying .Net Framework, you can easily create and send an  e-mail from the command line. 

       
#this works, just sends a simple email
$smtpServer = "your.webmailserver.com"
$smtpFrom = "fromadress@webmailserver.com"
$smtpTo = "todaddress@webmailserver.com"
$smtpCc = 'ccdaddress@webmailserver.com'
$smtpSubject = "This is a test email"
$username = "username"
$password = "password"
$body = "your mail body goes here"

$smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer
$smtp.Credentials = New-Object system.net.networkcredential($username, $Password);
$smtpBody = $body

$SMTPMessage = New-Object System.Net.Mail.MailMessage($smtpFrom, $smtpTo, $smtpSubject, $smtpBody)
$SMTPMessage.cc.Add($smtpCc)

$SMTPMessage.IsBodyHTML=$true
$smtp.Send($SMTPMessage)

You can run this script directly in PowerShell or you can just save the code in a file and save as PowerShell script (with .ps1 file extension) and run the script from Command prompt with following command.

       
PowerShell.exe -NoProfile -ExecutionPolicy Bypass C:\yourscriptfilename.ps1 



Comments

Popular posts from this blog

Different use of AL Find Functions for Filters in Dynamics 365 Business Central

How to create documentation comment using Custom Snippets in VS Code for AL - D365 Business Central.

Microsoft Dynamics NAV - Creating and Consuming a Codeunit Web Service