2016年10月17日 星期一

Powershell - Windows 10 send email

Powershell - Windows 10 send email
2016-10-17 

password.ps1
==========================================================================
"password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\sendemail\EmailPassword.txt"

sendemail.ps1
==========================================================================
$User = "xxxxxxxx@gmail.com"
$File = "C:\sendemail\EmailPassword.txt"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
$EmailTo = "xxxxxxxx@gmail.com"
$EmailFrom = "xxxxxxxxx@gmail.com"
$Subject = "ownCloud Windows was reboot" 
$Body = "ownCloud Windows was reboot" 
$SMTPServer = "smtp.gmail.com" 
$filenameAndPath = "C:\sendemail\Message.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password); 

$SMTPClient.Send($SMTPMessage)

error message
============================================================================
Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 

應用程式和裝置採用安全性較低的登入技術
=========================================================================
https://www.google.com/settings/security/lesssecureapps







沒有留言:

張貼留言