Skip to content

Instantly share code, notes, and snippets.

@kparms
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save kparms/9468148 to your computer and use it in GitHub Desktop.

Select an option

Save kparms/9468148 to your computer and use it in GitHub Desktop.
Email in Powershell
function sendMail([string]$arg1){
Write-Host "Sending Email"
#SMTP server name
$smtpServer = "SMTPSERVERNAME"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "from@address.com"
$msg.To.Add("to@address.com")
$msg.subject = "My Subject"
$msg.body = $arg1
#attachment (if necessary)
$logfile = "d:\folder\file.txt"
$attach = new-object Net.Mail.Attachment($logfile)
$msg.Attachments.Add($attach)
#Sending email
$smtp.Send($msg)
Write-Host "Email Sent"
}
sendMail("This is my message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment