I have been trying to create an email which will be sent from a gmail account when a PoweShell file is run. However I am unable to connect to the server when using the send-MailMessage
method.
I have the following code:
$from = "blondedavid@gmail.com"
$to = "blondedavid@gmail.com"
$subject = "The subject of your email"
$body = "This is just a test mail to verify the working of CMD"
$attachment = "C:\Users\37.csv"
$smtp = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("blondedavid@gmail.com", "xxx");
send-MailMessage -SmtpServer $smtp -Credential $SMTPClient -To $to -From $from -Subject $subject -Body $body -Attachment $attachment -Priority high
Any help would be greatly appreciated.