Send email with PowerShell

PowerShell
4 Comments

Ever want to send an email with PowerShell?  Well now you can! Simply add the code below to the end of a PowerShell script.  PowerShell will then use the Net.Mail.SmtpClient object to send the email. That’s it! Don’t forget to but in your appropriate information.

# Define and create the message
$strTo = "SendtoEmailAddress"
$strFrom = "FromEmailAddress"
$strSubject = "Email Subject"
$strBody = "Body of text for email"
$objMessage = New-Object System.Net.Mail.MailMessage –ArgumentList $strFrom, $strTo, $strSubject, $strBody

# Add an attachment
$strAttachment = "C:\Attachment\location"
$objAttachment = New-Object System.Net.Mail.Attachment –ArgumentList $strAttachment, "Application/Octet"
$objMessage.Attachments.Add($objAttachment)

# Define the SMTP server
$strSMTPServer = "smtp.server.com"
$objSMTP = New-Object System.Net.Mail.SMTPClient –ArgumentList $strSMTPServer

# Send the message
$objSMTP.Send($objMessage)

Read Comments

Menu