powershell - How to pass credentials to the Send-MailMessage command for sending emails -


i'm having difficulty passing credentials send-mailmessage command

this running:

send-mailmessage -smtpserver smtp.gmail.com -from 'myself@gmail.com' `     -to 'myself@gmail.com' -subject 'test' -attachment c:\cdf.pdf 

it errors below message because have not passed gmail credentials

send-mailmessage : smtp server requires secure connection or client not authenticated. server response was: 5.7.0 must issue starttls command first. 

i googled bit , went through man page of send-mailmessage , found "-credential" parameter needs passed.

my issue is: how ?

i tried get-credentials below:

$mycredentials = get-credential 

then entered usrname , password gmail in box pops up.

then run below command:

send-mailmessage -smtpserver smtp.gmail.com -credentail $mycredentials `   -from 'myself@gmail.com' -to 'myself@gmail.com' -subject 'test' -attachment c:\cdf.pdf 

and still fails exact same error.

so need guys on how pass credentials send-mailmessage command. learned pscredentials not sure , how use in context.

i found blog site: adam kahtava
found question: send-mail-via-gmail-with-powershell-v2s-send-mailmessage
problem is, neither of them addressed both needs (attachment password), did combination of 2 , came this:

$emailto = "myself@gmail.com" $emailfrom = "me@mydomain.com" $subject = "test"  $body = "test body"  $smtpserver = "smtp.gmail.com"  $filenameandpath = "c:\cdf.pdf" $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("username", "password");  $smtpclient.send($smtpmessage) 

since love make functions things, , need practice can get, went ahead , wrote this:

function send-email {     param (         [parameter(`             mandatory=$true)]         [string]$emailto,         [parameter(`             mandatory=$true)]         [string]$subject,         [parameter(`             mandatory=$true)]         [string]$body,         [parameter(`             mandatory=$true)]         [string]$emailfrom="myself@gmail.com",  #this gives default value $emailfrom command         [parameter(`             mandatory=$false)]         [string]$attachment,         [parameter(`             mandatory=$true)]         [string]$password     )          $smtpserver = "smtp.gmail.com"          $smtpmessage = new-object system.net.mail.mailmessage($emailfrom,$emailto,$subject,$body)         if ($attachment -ne $null) {             $smtpattachment = new-object system.net.mail.attachment($attachment)             $smtpmessage.attachments.add($stmpattachment)         }         $smtpclient = new-object net.mail.smtpclient($smtpserver, 587)          $smtpclient.enablessl = $true          $smtpclient.credentials = new-object system.net.networkcredential($emailfrom.split("@")[0], $password);          $smtpclient.send($smtpmessage)         remove-variable -name smtpclient         remove-variable -name password  } #end function send-email 

to call it, use command:

send-email -emailto "myself@gmail.com" -body "test body" -subject "test subject" -attachment "c:\cdf.pdf" -password "passowrd" 

i know it's not secure putting password in plainly that. i'll see if can come more secure , update later, @ least should need started. have great week!

edit: added $emailfrom based on juanpablo's comment


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -