jafar54
asked on
DotNetMethodException error on $mail.SendAndSaveCopy()
Keep getting this error on my Exchange 2010 SP1 Lab. I am tring to run the script below to send email via powershell:
Error:
[PS] C:\Mailgen>.\test.ps1
Exception calling "SendAndSaveCopy" with "0" argument(s): "The request failed. The underlying connection was closed: An unexpected error occurred on a send." At C:\Mailgen\test.ps1:32 char:22
+ $mail.SendAndSaveCopy <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Script:
#Script references
#
#Mike Pfeiffer
#URL: http://www.mikepfeiffer.net/2010/04/sending-email-with-powershell-and-the-ews-managed-api/
#
#EWS Managed API
#URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=C3342FB3-FBCC-4127-BECF-872C746840E1&displaylang=en&displaylang=en
$dllfolderpath = "C:\Program Files\Microsoft\Exchange\W eb Services\1.2\Microsoft.Exc hange.WebS ervices.dl l"
Add-Type -Path $dllfolderpath
$sid = [System.Security.Principal .WindowsId entity]::G etCurrent( ).User.Val ue
$user = [ADSI]"LDAP://<SID=$sid>"
$service = New-Object Microsoft.Exchange.WebServ ices.Data. ExchangeSe rvice
$service.AutodiscoverUrl($ user.Prope rties.mail )
$PrimarySmtpAddress = "user@test.com"
if($PrimarySmtpAddress.Get Type().ful lname -eq "Microsoft.Exchange.Data.S mtpAddress ") {
$Recipient = $PrimarySmtpAddress.ToStri ng()
}
else {
$Recipient = $PrimarySmtpAddress
}
$mail = New-Object Microsoft.Exchange.WebServ ices.Data. EmailMessa ge($servic e)
$mail.Subject = "Test"
$mail.Body = "Test"
[Void] $mail.ToRecipients.Add($Re cipient)
$mail.SendAndSaveCopy()
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Error:
[PS] C:\Mailgen>.\test.ps1
Exception calling "SendAndSaveCopy" with "0" argument(s): "The request failed. The underlying connection was closed: An unexpected error occurred on a send." At C:\Mailgen\test.ps1:32 char:22
+ $mail.SendAndSaveCopy <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Script:
#Script references
#
#Mike Pfeiffer
#URL: http://www.mikepfeiffer.net/2010/04/sending-email-with-powershell-and-the-ews-managed-api/
#
#EWS Managed API
#URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=C3342FB3-FBCC-4127-BECF-872C746840E1&displaylang=en&displaylang=en
$dllfolderpath = "C:\Program Files\Microsoft\Exchange\W
Add-Type -Path $dllfolderpath
$sid = [System.Security.Principal
$user = [ADSI]"LDAP://<SID=$sid>"
$service = New-Object Microsoft.Exchange.WebServ
$service.AutodiscoverUrl($
$PrimarySmtpAddress = "user@test.com"
if($PrimarySmtpAddress.Get
$Recipient = $PrimarySmtpAddress.ToStri
}
else {
$Recipient = $PrimarySmtpAddress
}
$mail = New-Object Microsoft.Exchange.WebServ
$mail.Subject = "Test"
$mail.Body = "Test"
[Void] $mail.ToRecipients.Add($Re
$mail.SendAndSaveCopy()
--------------------------
ASKER
I am running this on Exchange 2010 SP1. I have the EWS Managed API 1.2 installed.
ok
this script is looking for the API of a 64 bits edition of the files (C:\Program Files)
do you have installed this api in its x64 or x86 version ('C:\Program Files' or 'C:\Program Files (x86)' ) ?
this script is looking for the API of a 64 bits edition of the files (C:\Program Files)
do you have installed this api in its x64 or x86 version ('C:\Program Files' or 'C:\Program Files (x86)' ) ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't know this script but here are some ideas:
- Are you sure you have installed the prerequisite for the script, look at the line :
$dllfolderpath = "C:\Program Files\Microsoft\Exchange\W
and its previous link states:
#EWS Managed API
#URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=C3342FB3-FBCC-4127-BECF-872C746840E1&displaylang=en&displaylang=en
the link is broken but the new download link for this API (E2010 sp3) is:
http://www.microsoft.com/en-us/download/details.aspx?id=28952
Install it and retry