{"id":1006,"date":"2017-11-06T13:25:06","date_gmt":"2017-11-06T11:25:06","guid":{"rendered":"http:\/\/dety.net.ua\/?p=1006"},"modified":"2017-11-06T13:25:06","modified_gmt":"2017-11-06T11:25:06","slug":"powershell-send-and-read-email","status":"publish","type":"post","link":"https:\/\/dety.net.ua\/?p=1006","title":{"rendered":"Powershell: send and read email"},"content":{"rendered":"<ol>\n<li>In your Gmail account we have to turn the feature &#8220;Less secure app&#8221; on.<\/li>\n<li>To read mail I used the fine tool for PS: <a href=\"https:\/\/github.com\/nikoblag\/Gmail.ps\">https:\/\/github.com\/nikoblag\/Gmail.ps<\/a><\/li>\n<\/ol>\n<p>Script example:<\/p>\n<blockquote><p>#==============================================================<br \/>\n# Mail<br \/>\nfunction global:Send-Email ($recipient,$SSubject,$BBody) {<br \/>\n$email = &#8220;your.mail@gmail.com&#8221;<br \/>\n$pass = &#8220;password&#8221;<br \/>\n$smtpServer = &#8220;smtp.gmail.com&#8221;<br \/>\n$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force)<br \/>\n$From = &#8220;your.mail@gmail.com&#8221;<br \/>\n$To = $recipient<br \/>\n$Cc = &#8220;YourBoss@YourDomain.com&#8221;<br \/>\n$Subject = $SSubject<br \/>\n$Body = $BBody<br \/>\n$SMTPServer = &#8220;smtp.gmail.com&#8221;<br \/>\n$SMTPPort = &#8220;587&#8221;<br \/>\n$encoding = [System.Text.Encoding]::UTF8<br \/>\nSend-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials -Encoding $encoding -Attachments $Attachment<br \/>\n}<br \/>\n#==============================================================<\/p>\n<p>&nbsp;<\/p>\n<p>$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force)<br \/>\n$gmail = New-GmailSession -Credential $Credentials<br \/>\n$inbox = $gmail | Get-Mailbox<br \/>\n#$inbox | Get-Message -Unread<\/p>\n<p>$message = $inbox | Get-Message -Unread -Subject &#8220;DeleteALL&#8221;<br \/>\nif ($message) {<br \/>\nif ($message.Subject -eq &#8220;DeleteALL&#8221;) {<br \/>\n# Do something<br \/>\necho &#8220;The message was detected, deleting the message and start the process &#8230;&#8221;<br \/>\nRemove-Message -Session $gmail -Message $message<br \/>\necho &#8220;Sending warning email &#8230;&#8221;<br \/>\nSend-Email $message.From &#8220;WARNING!!!&#8221; &#8220;Running&#8221;<br \/>\n# Delete ALL<br \/>\necho &#8220;Deleting &#8230;&#8221;<br \/>\nRemove-Item -Recurse -Force D:\\Kill<br \/>\necho &#8220;Deleting &#8230;&#8221;<br \/>\necho &#8220;Shredding &#8230;&#8221;<br \/>\nStart-Process &#8220;cipher W:d:\\&#8221;<br \/>\n$text = Get-Content C:\\Backup\\report.txt | Out-String<br \/>\nSend-Email $message.From &#8220;Finita!!!&#8221; $text<br \/>\n}}<br \/>\nelse {<br \/>\necho &#8220;Fuhhh. No kill-messages&#8221;<br \/>\n$inbox | Get-Message -Unread<br \/>\n}<\/p>\n<p>Remove-GmailSession $gmail<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>In your Gmail account we have to turn the feature &#8220;Less secure app&#8221; on. To read mail I used the fine tool for PS: https:\/\/github.com\/nikoblag\/Gmail.ps Script example: #============================================================== # Mail function global:Send-Email ($recipient,$SSubject,$BBody) { $email = &#8220;your.mail@gmail.com&#8221; $pass = &#8220;password&#8221; $smtpServer = &#8220;smtp.gmail.com&#8221; $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force) $From [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,18],"tags":[],"class_list":["post-1006","post","type-post","status-publish","format-standard","hentry","category-novosti","category-powershell"],"_links":{"self":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1006"}],"version-history":[{"count":1,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions"}],"predecessor-version":[{"id":1007,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions\/1007"}],"wp:attachment":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}