Link to home
Start Free TrialLog in
Avatar of jafar54
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\Web Services\1.2\Microsoft.Exchange.WebServices.dll"

Add-Type -Path $dllfolderpath

$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$user = [ADSI]"LDAP://<SID=$sid>"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$service.AutodiscoverUrl($user.Properties.mail)

$PrimarySmtpAddress = "user@test.com"

if($PrimarySmtpAddress.GetType().fullname -eq "Microsoft.Exchange.Data.SmtpAddress") {
    $Recipient = $PrimarySmtpAddress.ToString()
}
else {
    $Recipient = $PrimarySmtpAddress
}
$mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$mail.Subject = "Test"
$mail.Body = "Test"
[Void] $mail.ToRecipients.Add($Recipient)
$mail.SendAndSaveCopy()


--------------------------------------------------------------------------------
Avatar of Jean-Bernard VERNEJOUX
Jean-Bernard VERNEJOUX
Flag of France image

Are you running your script ON the Exchange 2010 Server ?

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\Web Services\1.2\Microsoft.Exchange.WebServices.dll"

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
Avatar of jafar54
jafar54

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)' ) ?
ASKER CERTIFIED SOLUTION
Avatar of jafar54
jafar54

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial