Skip to content

Instantly share code, notes, and snippets.

@artem78
Last active March 21, 2019 12:26
Show Gist options
  • Save artem78/e68b2f77bfdb5674d5ad0c70f74f4cee to your computer and use it in GitHub Desktop.
Save artem78/e68b2f77bfdb5674d5ad0c70f74f4cee to your computer and use it in GitHub Desktop.
Sending email with attachment from Windows using PowerShell
$User = "[email protected]"
$Password = "mypass"
$From = $User
$To = "[email protected]"
$Subject = "Log"
$Body = "Here is your log file"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$mail = New-Object system.net.mail.mailmessage
$mail.from = $From
$mail.To.add($To)
$mail.Subject = $Subject
$mail.Body = $Body
$attach = New-Object System.Net.Mail.Attachment("log.txt")
$mail.Attachments.Add($attach)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($User, $Password)
$SMTPClient.Send($mail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment