Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Credential use in Send-MailMessage, can't get it to work programmatically

$
0
0

I have been working on this for 4 months and can't make it work except interactively, i.e., I have to enter my login name and pwd, manually.  I got this code from the forum from "jrv". Here's my code:


##This works perfectly but interactively! I even received and read the message.

$mailprops = @{
        ##User = 'me@mysite.com'
        ##Pword = ConvertTo-SecureString -String "xxxxxxxx" -AsPlainText -Force
        ##Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword

    From = 'me@verizon.net'
    To = 'you@verizon.net'
    Subject = 'PowerShell test of sending an email using your-site in e93.ps1'
    Body = 'PowerShell test of sending an email using your-site in e93.ps1'
    Attachments = 'D:\DOWNLOAD\xcopy.txt'
    SMTPServer = 'smtp.your-site.com'
    Port = '587'
    UseSSL = $true
    Credential = Get-Credential

    ##Credential = 'Cred'
    ##Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "them@verizon.net", "yyyyyyyyy"
    ##Credential = 'Cred'
    ##Credential = "www.gmail.com\mygmail"
    ##Credential = 'PSCredential'
}

Send-MailMessage @mailprops

------------------------------------------------
##This doesn't work from the PowerShell cmdline!

$mailprops = @{
    User = 'me@mysite.com'
    Pword = ConvertTo-SecureString -String "xxxxxxxxx" -AsPlainText -Force
    Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList User, Pword
    From = 'me@verizon.net'
    To = 'you@verizon.net'
    Subject = 'PowerShell test of sending an email using your-site in e93.ps1'
    Body = 'PowerShell test of sending an email using your-site in e93.ps1'
    Attachments = 'D:\DOWNLOAD\xcopy.txt'
    SMTPServer = 'smtp.your-site.com'
    Port = '587'
    UseSSL = $true
    Credential = 'Cred'

        ##Credential = Get-Credential
        ##Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "them@verizon.net", "yyyyyyyyy"
    ##Credential = 'Cred'
    ##Credential = "www.gmail.com\mygmail"
    ##Credential = 'PSCredential'
}

Send-MailMessage @mailprops

------------------------------------------------
Here is the run and the errors:

PS D:\DOWNLOAD\PROGRAMS\POWERSHELL> .\e93

New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e93.ps1:11 char:12
+     Cred = New-Object -TypeName System.Management.Automation.PSCreden ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At D:\DOWNLOAD\PROGRAMS\POWERSHELL\e93.ps1:28 char:18
+ Send-MailMessage @mailprops
+                  ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
------------------------------------------------

This is driving me nuts. Just can't get it.  Any Help, that actually works, will be greatly appreciated!  
Thank you all Greatly in advance!
Crank


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>