Send email with attachment – PowerShell

To totally unlock this section you need to Log-in


Login

Sending an email with PowerShell can be a great way to deliver data under the correct circumstances. It takes some additional work compared to exporting a file, but the process can be simple with the proper reference.

From the console

Often, PowerShell will give you the results you need in the console window only, or you can specify an output file or set-content location to store the results of a script. While this often is sufficient, sending an email with your PowerShell script can be a great way to deliver data.

There are a few properties and variables you’ll want to define — the first is the IP address or hostname of the server you will want the e-mail relayed through. If you choose to run the script from the e-mail server, for example, you can use the localhost address.

$smtpServer = “127.0.0.1”

We will also reference the file we wish to attach — this may have been created earlier in the script, it may be a static filename that’s replaced on a regular basis, or it may be dynamic based on your needs.

$file = “C:\folder\file.csv”
$att = new-object Net.Mail.Attachment($file)

Then, we’ll import the necessary Microsoft .NET objects.

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

Now we’ll fill in the other properties of the email. The “from” address can be of particular importance based on your organization’s email filtering and spam policies. You can use variables to fill in any of the below values.

$msg.From = “[email protected]com”

You can add as many of the following lines as you want to add multiple recipients to the email.

$msg.To.Add(“[email protected]”)
$msg.To.Add(“[email protected]”)

Set the subject of the email with this line.

$msg.Subject = “Notification from email server”

Use the @” to open and “@ to close a multi-line string. It’s likely that the body of the e-mail will be multiple lines, though if it isn’t, you can use regular double quotes.

$msg.Body = @”This is an example
message

body”@
Now, attach the file defined above.

$msg.Attachments.Add($att)

The following two lines will send the message and then remove the attachment from memory. Failing to dispose the attachment will result in unnecessary use of the system’s memory, which may eventually impact system performance.

$smtp.Send($msg)
$att.Dispose()

Example of a base e-mail

Below is a very simple example of sending an email using a Powershell script:

$message = @”
Hello,
This is a sample e-mail body part to send with Powershell.
“@

$smtpserver=”smtp-server-name”


$emailTo = “[email protected]
$emailFrom = “[email protected]
$subject=”WARNING | WWW | Low Disk Space”

$msg = new-object Net.Mail.MailMessage

$msg.To.Add($emailTo)
$msg.From = $emailFrom
$msg.Subject = $subject
$msg.Body = $message

$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)

The following code, instead, will show how to send an e-mail message with an attachment (from the local file system):

$date = Get-Date
$message = @”
There is a new log for you.

This mail has been generated at $date.
“@

$smtpserver=”10.4.13.71″


$emailTo = “[email protected]
$emailFrom = “[email protected]
$subject=”New log for you!”

$msg = new-object Net.Mail.MailMessage
$msg.Attachments.Add(“C:\scripts\logtosend.txt”)
$msg.To.Add($emailTo)
$msg.From = $emailFrom
$msg.Subject = $subject
$msg.Body = $message

$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)
$msg.Dispose()

2 thoughts on “Send email with attachment – PowerShell”

  1. Sending mails for notifications or alarms is very common in many fields, especially when we can’t access a real time dashboard to check functionalities. So, in Powershell this is a very simple way to code a sending mail process.

Comments are closed.