Last active
March 21, 2019 12:26
-
-
Save artem78/e68b2f77bfdb5674d5ad0c70f74f4cee to your computer and use it in GitHub Desktop.
Sending email with attachment from Windows using PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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