Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

EWS - Error subscribing to streaming notifications

$
0
0

I'm using the sample code I was pointed to at Glen's Exchange Dev Blog, to open up streaming subscriptions for new mail notifications via EWS. I've changed it a little bit because I want to listen to the main inbox as opposed to a particular folder so I've changed SubscribeToStreamingNotifications to SubscribeToStreamingNotificationsOnAllFolders, the .dll path is different as it's version 2, and obviously the mailbox and credentials have been set. I'm just trying to get it working before I actually do something with it.

$MailboxName = "user@domain.com"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$service.TraceEnabled = $false

$service.Credentials = New-Object System.Net.NetworkCredential("user@domain.com","password")
$service.AutodiscoverUrl($MailboxName ,{$true})

$fldArray = new-object Microsoft.Exchange.WebServices.Data.FolderId[] 1
$Inboxid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$fldArray[0] = $Inboxid
$stmsubscription = $service.SubscribeToStreamingNotificationsOnAllFolders($fldArray, [Microsoft.Exchange.WebServices.Data.EventType]::NewMail)
$stmConnection = new-object Microsoft.Exchange.WebServices.Data.StreamingSubscriptionConnection($service, 30);
$stmConnection.AddSubscription($stmsubscription)
Register-ObjectEvent -inputObject $stmConnection -eventName "OnNotificationEvent" -Action {
	foreach($notEvent in $event.SourceEventArgs.Events){	
		[String]$itmId = $notEvent.ItemId.UniqueId.ToString()
		$message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($event.MessageData,$itmId)"Subject : " + $message.Subject + " " + (Get-Date) | Out-File c:\temp\log2.txt -Append 
	} 
} -MessageData $service
Register-ObjectEvent -inputObject $stmConnection -eventName "OnDisconnect" -Action {$event.MessageData.Open()} -MessageData $stmConnection
$stmConnection.Open()

The error I'm getting is at line 13 and is saying that it 'Cannot convert argument "0", with value: "Inbox (user@domain.com)", for "SubscribeToNotificationsOnAllFolders" to type "Microsoft.Exchange.WebServices.Data.EventType": Cannot convert the "Inbox (user@domain.com)" value of type "Microsoft.Exchange.WebServices.Data.FolderId" to type "Microsoft.Exchange.WebServices.Data..EventType".

Then it tells me that two lines down, Exception calling "AddSubscription" with "1" argument(s): "Value cannot be null. Parameter name: subscription.
At line 15.

So somewhere (line 13 or above) I've not set a value and it's stayed as null - can anyone help me though because I can't really change the script too much as to how I need it until I can get it working from its base. When it's done I can post the results for others.

Thanks in advance.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



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