This worked for me:
$SMTPServer = "smtp.gmail.com" $SMTPPort = "587" $Username = "[email protected]" $Password = "" $to = "[email protected]" $cc = "[email protected]" $subject = "Email Subject" $body = "Insert body text here" $attachment = "C:\test.txt" $message = New-Object System.Net.Mail.MailMessage $message.subject = $subject $message.body = $body $message.to.add($to) $message.cc.add($cc) $message.from = $username $message.attachments.add($attachment) $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); $smtp.EnableSSL = $true $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message) write-host "Mail Sent"
However, you may get this error message:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "
This is because the default security settings of Gmail block the connection, as suggested by the auto message from Google. So just follow the instructions in the message and enable "Access for less secure apps". At your own risk. :)
More info here: http://petermorrissey.blogspot.ro/2013/01/sending-smtp-emails-with-powershell.html