Send Mail from PowerShell

Working with PowerShell the next step is to enable additional features in my scripts.  This script will  send email directly from within PowerShell.

Hope this helps someone.

[sourcecode language=”css”]

#################################################
# Name: SMTPAnonTest.ps1 #
# Author: Carlos Vargas #
# Email: cvargas at cavarpe dot com #
# Version: 1.0 #
# Purpose: Test Anonymous Relay #
#################################################
#################################################
# Requirements #
# 1. Set Powershell Script Execution #
# – Set-ExecutionPolicy RemoteSigned #
#################################################

###############
# Heading #
###############
Write-Host "SMTP Anonymous Test will help you test your anonymous SMTP Relay Setting"
Write-Host "Author: Carlos Vargas"
Write-Host ""
###############
# Variables #
###############
$smtpServer = Read-Host -Prompt "Type SMTP Server"
$mailfrom = Read-Host -Prompt "From"
$mailto = Read-Host -Prompt "To"
$mailsubject = Read-Host -Prompt "Subject"
$mailbody = Read-Host -Prompt "Body"

################
# Mail Process #
################
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $mailfrom
$msg.To.Add($mailto)
$msg.Subject = ($mailsubject)
$msg.Body = ($mailbody)
$smtp.Send($msg)

[/sourcecode]

Subscribe For Latest Updates

Sign up for best of digital marketing, livestream and technology opinions on what matters to you.

Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: