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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close