How To: Send an Email Through PowerShell Scripts
Typically, people use Outlook or other tool for emails. But if you want to be able to send an email without having to go into Outlook because it can cause you to get distracted, taking you away from what you’re working on, there is another method. This blog explains how to use a PowerShell script to send an email.
- Open your PowerShell ISE. Following are some specifications required for emailing:
- $EmailFrom : Specifies the sender information – The example in Figure 1 shows email@example.com sending an email to abc.01gmail.com
- $EmailTo: Specifies the recipient of the mail
- $subject: Specifies the subject of the email you want to send
- $Body: Specifies the body of the message
- $SMTPServer: The mail server you will be using to send the email
- $SMTPClient: The name of the client; create a newobject and call the system.netmail.api and pass the smtpserver info
- $SMTPClient.send: Specifies sending the email using .send command
Figure 1 - PowerShell script for sending an email
- Press F5 to run the script. You will receive a message box like that one shown in Figure 2.
Figure 2 – Message box indicating credentials are required
- Specify the password of your Gmail account and click OK.
- You can now check your email. Figure 3 shows what it looks like to receive an email.
Figure 3 – Email notification in PowerShell
If you have any questions about this or other Dynamics NAV or Business Central topic for any version, contact ArcherPoint.
Read more "How To" blogs from ArcherPoint for practical advice on using Microsoft Dynamics Business Central or NAV.
If you are interested in NAV/Business Central development, check out our collection of NAV Development Blogs.