I am using the send-MailMessage cmdlet in PowerShell version 2 and it gives me a error.
I tried using all the options which were mentioned in the earlier posts and it didn't help me.
This is the command that I am using:
send-MailMessage -from "[email protected]" -to "[email protected]"
-subject "test" -body"test" -smtp "smtp.gmail.com"
I get an error:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated
e was: 5.7.0 Must issue a STARTTLS command first. l46sm12283804qgd.27 - gsmtp
At line:1 char:17
+ send-MailMessage <<<< -from "[email protected]" -to "[email protected]" -subject "te
credential abc
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Se
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
I tried using UseSsl= true and also by giving credentials and even then I was not able to send email. Could you let me know what I am missing here?
In order to use gmail you need SSL, I'am not sure you can use Send-MailMessage try this :
$emailSmtpServer = "smtp.gmail.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "[email protected]"
$emailSmtpPass = "yourPassword"
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "[email protected]"
$emailMessage.To.Add("[email protected]")
$emailMessage.Subject = "Small mail for a friend"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p><strong>Hello me</strong>.</p>
<p>It seems to work</p>
<p>JP</p>
"@
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
Edited :
To use mail attachment please have a look Sytem.Net.Mail.MailMessage documentation. You need first to create an Attachment and then to add it to the MailMessage Attacments collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With