VMware PowerShell scripting - automated gmail - powershell

This works, but...
$EmailFrom = "vsphere#nasa.gov"
$EmailTo = "userwithOLDsnapshot#nasa.gov"
$Subject = "Notification"
$Body = "this is a notification for your snapshot"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Is there anyway of doing this with a credential store? Embedding creds in scripts in clear text is a no no.

The following will create an encrypted string. You can save this output to some file and read it in later:
$securePWord=Read-Host "Enter some password" -AsSecureString
This will convert a SecureString into usable text:
$pWord=[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($securePWord))

Related

Powershell email not including attachment?

I am trying to set up a batch file which will call a Powershell and ultimately send a file on the users computer to a given email address. While I can get the email to send, it always sends without the attachment included.
The batch file code i have is:
powershell.exe -executionpolicy bypass -file C:\Users\attachment2.ps1
and the Powershell code I have is:
$attachment = "C:\Users\SearchResults\37.csv"
$EmailFrom = "david#gmail.com"
$EmailTo = "john#gmail.com"
$Subject = "The subject of your email"
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$Body = "This is just a test mail to verify the working of CMD"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx", "xxxx");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $attach, $Body)
If anyone knows as to why the attachment is not being included it would be of great help. I have looked at other examples on stackoverflow as well as other sites but have been not able to fix my own code. Any help would be greatly appreciated.

Unable to send email using Gmail SMTP in Powershell. Error: smtpServer requires secure connection or client was not authenticated. How to fix? [duplicate]

i am trying to send emails though gmail smtp server using powershell script. but i am not able to send emails .. i am using below script-
$EmailFrom = "bttpm#gmail.com"
$EmailTo = "kamlesh.bhatt#domain.com"
$Subject = "Notification from XYZ"
$Body = "this is a notification from XYZ Notifications.."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("bttpm", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
This worked for me:
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "username#gmail.com"
$Password = ""
$to = "user1#domain.com"
$cc = "user2#domain.com"
$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
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
I recently ran into this issue with attempting to automate messages through my Gmail account. I do not like the option of allowing "access for less secure apps". I want forward thinking security. So I continued my search.
My solution was to enable "2-Step" verification. This provides a slightly more secure solution as it provides an alternate password for your script to access your account.
Sign in using App Passwords:
https://support.google.com/accounts/answer/185833
Stefan's link also has this solution, but it's buried in the comments and I didn't originally find it there until after I found it on my own through searching my Gmail account. That's why I am posting it here.
Send email with attachment using powershell -
$EmailTo = "udit043.ur#gmail.com" // abc#domain.com
$EmailFrom = "udit821#gmail.com" //xyz#gmail.com
$Subject = "zx" //subject
$Body = "Test Body" //body of message
$SMTPServer = "smtp.gmail.com"
$filenameAndPath = "G:\abc.jpg" //attachment
$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("udit821#gmail.com", "xxxxxxxx"); // xxxxxx-password
$SMTPClient.Send($SMTPMessage)

Sending E-Mail with attachment in PowerShell

I want to send a CSV file in this e-mail.
I can send the email with no problem, but I tried a lot to send attachment with it, but at the end the e-mail was sent without the attachment.
function sendmail($Body) {
$Smtp = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$Smtp.Host = "smtp-server"
$MailMessage.From = "sender#example.com"
$MailMessage.To.Add("recipient#example.org")
$MailMessage.Subject = "Hello"
$MailMessage.Body = $Body
$smtp.Port = 25
$smtp.EnableSsl = $false
$Smtp.Send($MailMessage)
}
$Body = "12334"
sendmail $Body
The e-mail just have to look like this:
Hello, all the information are in the file Example.csv
Send-MailMessage has a direct way of handling the attachments. Below is the sample.
Send-MailMessage -From "User01 <user01#example.com>" `
-To "User02 <user02#example.com>", `
"User03 <user03#example.com>" `
-Subject "Sending the Attachment" `
-Body "Forgot to send the attachment. Sending now." `
-Attachment "data.csv" -SmtpServer smtp.fabrikam.com
OR, you should use System.Net.Mail.MailMessage, then you have to use:
$EmailFrom = "<user#domain.tld>"
$EmailTo = "<user#domain.tld>"
$EmailSubject = "<email subject"
$SMTPServer = "smtphost.domain.tld"
$SMTPAuthUsername = "username"
$SMTPAuthPassword = "password"
$emailattachment = "<full path to attachment file.csv>"
function send_email {
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.From = ($emailfrom)
$mailmessage.To.Add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
}
Refer THIS if required.

Adding a CC to a email being sent from powershell

I'm sending a report out via email in Powershell and I would like to add a CC, but everything I've tried thusfar isnt working.
heres my sendmail code...
$ExportedReport = "$WorkFolder\InactiveUserReport.csv"
$EmailTo = "myemail#domain.com"
$EmailFrom = "reportsemail#domain.com"
$Subject = "REPORT BLAH BLAH"
$Body = "blah blah blah."
$SMTPServer = "mysmtpserver"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($ExportedReport)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myusername", "mypassword");
$SMTPClient.Send($SMTPMessage)
Any help on how I can modify this to add a CC would be wonderful!
Your MailMessage has a CC property.
Use the following:
$SMTPMessage.cc.Add("cc.address#domain.tld")

Authentication failure when sending message

I am trying to send a message with PowerShell but I can not send the message. The error says:
The server SMTP requires a connection safe or the user didn't authenticate.
I don't know where is my error and I don't know what more I can try.
This is my code:
$EmailTo = "prueba#gmail.com"
$EmailFrom = "pacopepe#gmail.com"
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("pacopepe#gmail.com", "pacopaco");
$SMTPClient.Send($SMTPMessage)
Note: In credentials, I put $emailfrom and the password of his mail.