0

Error

New-Object : Cannot find an overload for "PSCredential" and the argument count: "2". At D:\Scripts\gsend.ps1:12 char:15

Code

#Create Password File (Only need once) #$Credential = Get-Credential #$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt" #Send Email $EncryptedCredential = "D:\scripts\gsendcred.txt" $EmailUsername = "[email protected]" $EncryptedPW = Get-Content "D:\scripts\gsendcred.txt" $EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential - AsPlainText -Force $Credential = New-Object Management.Automation.PSCredential ($EmailUsername, $EncryptedPW) $EmailFrom = "[email protected]" $EmailTo = "[email protected]" $EmailSubject = "GSEND Test Subject" $EmailBody = "Test Body" $SMTPServer = "smtp.gmail.com" $SMTPPort = 587 $SMTPSsl = $true 
6
  • Here is the error I receive: New-Object : Cannot find an overload for "PSCredential" and the argument count: "2". At D:\Scripts\gsend.ps1:12 char:15 Commented Sep 14, 2017 at 13:05
  • 2
    Welcome to SO. Please read this content before you post anything more to SO: stackoverflow.com/help/asking Commented Sep 14, 2017 at 13:08
  • 1
    Use your $EncryptedCredential instead of $EncryptedPW, the PSCredential Object needs a SecureString Commented Sep 14, 2017 at 13:18
  • Cannot find overload means that what you put inside the parenthesis isn't what the method is looking for. So that means that something is wrong with $EmailUsername or $EncryptedPW. As Olaf says, this PSCredentials takes a string username and secure string password. $EncryptedPW isn't a secure string. Commented Sep 14, 2017 at 13:25
  • Changing to EncryptedCredential runs without errors now. The email does not send though. I added $param to the bottom of the script and and it errors out. (SIDE NOTE NEWBIE HERE to PowerShell). Here is what I added to the bottom: $param = @{SmtpServer = $SMTPServerPort = $SMTPPort, $UseSsl = $SMTPSsl, $Credential = $Credential $From = $EmailFrom $To = $EmailTo $Subject = $EmailSubject $Body = $EmailBody $Attachments = $EmailAttachments} Send-MailMessage @param Commented Sep 14, 2017 at 15:03

2 Answers 2

1

I believe your issue is handling credentials. Here is how I handle the smtp credentials:

$smtpPwd = "password" $smtpCredentialsUsername = "[email protected]" $smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force $Credentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword 

Then:

 $smtp.Credentials = $Credentials 

Or if you are using Send-MailMessage store your email credentials locally by running this short script by itself beforehand:

Get-Credential | Export-Clixml C:\fso\myemailcreds.xml 

It will prompt you to enter your credentials and then store them locally in (in this case) a folder called fso. Then in any Send-MailMessage cmdlet use the locally stored credentials as follows:

Send-MailMessage -To 'Recipient <[email protected]>' -from 'John Donnelly <[email protected]>' -cc 'whoever <[email protected]>' -Subject $subject -Body $body -BodyAsHtml -smtpserver "smtp.office365.com" -usessl -Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks John, but it still doesn't correct my issue with my own script. Mail still does not go out
Can you post the entire script?
John, below is the script:
#Create Password File (Only need once) #$Credential = Get-Credential #$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt" #Send Email $EncryptedCredential = "D:\scripts\gsendcred.txt" $EmailUsername = "[email protected]" $EncryptedPW = Get-Content "D:\scripts\gsendcred.txt" $EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential - AsPlainText -Force $Credential = New-Object Management.Automation.PSCredential ($EmailUsername, $EncryptedPW) $EmailFrom = "[email protected]" $EmailTo = "[email protected]"
$EmailSubject = "GSEND Test Subject" $EmailBody = "Test Body" $SMTPServer = "smtp.gmail.com" $SMTPPort = 587 $SMTPSsl = $true
|
0

You Can use below Powershell script for send mail

Send-MailMessage -To "[email protected]" -From "[email protected]" -Cc "[email protected]","[email protected]" -Subject Test Mail -BodyAsHtml $htmlbody -SmtpServer "mailhost.abc.com"

where $htmlbody is string variable that contain html.

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.