2

I am trying to send an email from my gmail ccoiunt in just one single line using windows 8 and powersell. This is the code I use:

Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from '[email protected]' -to '[email protected]' -subject 'Test' -body 'Body' –UseSsl 

But I don't know how to add the credentials. How can I add username and password to this single code line? (there is no need to encrypt the password).

Thanks

3
  • possible duplicate of How to pass credentials to the Send-MailMessage command for sending emails Commented Jul 10, 2013 at 9:38
  • It is not a duplicate because "-credential $mycredentials" doesn't have the credentials in just a single code line. I don't know how to add username and password to that "-credential" command Commented Jul 10, 2013 at 14:56
  • 1
    Try reading the answers, not just the question. Commented Jul 10, 2013 at 15:30

2 Answers 2

2
Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from '[email protected]' -to '[email protected]' -subject 'Test' -body 'Body' –UseSsl -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUsername", (ConvertTo-SecureString -String "myPassword" -AsPlainText -Force)) 
Sign up to request clarification or add additional context in comments.

2 Comments

Need a comma between "myUserName" and (ConvertTo-Secure -String "myPassword" -AsPlainText -Force)) since it is an ArgumentList
@quasar Good eye, thanks for catching it. I've edited it accordingly.
1
 $EmailTo = "[email protected]" // [email protected] $EmailFrom = "[email protected]" //[email protected] $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("[email protected]", "xxxxxxxx"); // xxxxxx-password $SMTPClient.Send($SMTPMessage) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.