Spedire mail da GMAIL usando powershell
Il codice per spedire mail tramite powershell e' molto semplice, ma nasconde qualche inghippo. Alla fine, a forza di sbatterci il cranio e leggendo qua e la sono riuscito nell'intento. Ecco il codice che ho utilizzato:
I problemi maggiori li ho avuti cercando di passare le credenziali
Fino a che non ho usato la riga di codice sopra, non c'e' stato verso di riuscirci.
I problemi maggiori li ho avuti cercando di passare le credenziali
Fino a che non ho usato la riga di codice sopra, non c'e' stato verso di riuscirci.
#Mail
$From = "sourceaddress@gmail.com"
$To = "xxx@domain1.it , xxx@domain2.com"
$Cc = "sourceaddress@gmail.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = "prova invio"$Body = "vediamo se va"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
#Credentials
$Username = "sourceaddress@gmail.com"
$Password = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force$
Credential = New-Object System.Management.Automation.PSCredential $Username, $Password
#Send MailSend-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Credential
$From = "sourceaddress@gmail.com"
$To = "xxx@domain1.it , xxx@domain2.com"
$Cc = "sourceaddress@gmail.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = "prova invio"$Body = "vediamo se va"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
#Credentials
$Username = "sourceaddress@gmail.com"
$Password = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force$
Credential = New-Object System.Management.Automation.PSCredential $Username, $Password
#Send MailSend-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Credential
$Password = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
Alla fine tutto ha magicamente funzionato
p.s. occorre ricordare di consentire l'utilizzo da parte di app meno sicure per l'account in questione. Questa operazione si fa accedendo con il proprio account al seguente link:
https://myaccount.google.com/lesssecureapps?pli=1
https://myaccount.google.com/lesssecureapps?pli=1
FALLIMENTO
Non sono assolutamente riuscito usando il metodo sotto (che usa System.Net.Mail.SmtpClient anziche send-mailmessamege cmdlet). Se qualcuno avesse voglia di menarselo ancora un po... Io l'ho fatto abbastanza, almeno per adesso :)
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "465"
$Username = "sourceaddress@gmail.com"
#$Password = "mypassword"
$Password = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
$to = "email_to"
$cc = "email_cc"
$subject = "Results for AD Report LAST LOGON"
$body = "Script version 1.0 by Maarten Mol"
#$attachment = "C:\Test\Users-Last-Logon.csv"
$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"