• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1504
  • Last Modified:

Exchange 2007 Web Services Error with VBScript

Ok so I'm a VBScript guy and never learned .NET so that being said what I'm trying to do is connect to a particular mailbox and download attachments from any email in the inbox.  I've made some real good progress with the Exchange Web Services SOAP requests.  I can use the MSXML2.XMLHTTP object to POST to the URL of https://MAILSERVER/ews/exchange.asmx and I'm posting the XML listed in Microsofts documentation for certain functions  (http://msdn.microsoft.com/en-us/library/aa494316(v=exchg.140).aspx).  

I can view all the email in the inbox (FindItem), I can view the details of each email (GetItem) but I'm failing when I'm trying to get the details of the attachment from the email (GetAttachment).  The error message I'm getting is:

Server was unable to process request.  System.Xml.XmlException:  'xmlns' is an unexpected token.  Expecting white space.  Line 1, position 107.

I think this is saying the error is in the response but I can't see what the response text is so I have no idea what the error is.  My end goal is I'm simply trying to download the attachments (which will always be PDF's in my case) and I can only use VBScript.  Any help or direction would be GREATLY appreciated!  Here is my code:


const TARGETURL="https://MAILSERVER/ews/exchange.asmx"
const USERNAME="domain\username"
const PASSWORD="password"
GetAttachmentTemplate = "C:\GetAttachment.xml"

set fso=CreateObject("Scripting.FileSystemObject")

'---------------------------------------------------------
'	Read contents of GetAttachment.xml file
'---------------------------------------------------------
set oTS=fso.OpenTextFile(GetAttachmentTemplate)
sTemplate = ""
while not oTS.AtEndOfStream
	sTemplate=sTemplate & oTS.ReadLine
wend
oTS.Close


'---------------------------------------------------------
'	POST to Exchange Server for Attachment
'---------------------------------------------------------
set oXMLHTTP=CreateObject("MSXML2.XMLHTTP")
set oXML=CreateObject("MSXML2.DOMDocument")
	
oXMLHTTP.Open "POST", TARGETURL, false, USERNAME, PASSWORD
oXMLHTTP.SetRequestHeader "Content-Type", "text/xml"
oXMLHTTP.Send sTemplate
	
if oXMLHTTP.Status="200" then

	if oXML.LoadXML(oXMLHTTP.ResponseText) then
		set CreateItemResponse = oXML
		wscript.echo "It Worked"
	end if

else
	wscript.echo "It did NOT work.  Response status: " & oXMLHTTP.Status & VbCrLf & oXMLHTTP.StatusText & VbCrLf & oXMLHTTP.ResponseText
end if

Open in new window

0
tphelps19
Asked:
tphelps19
  • 2
1 Solution
 
tphelps19Author Commented:
Well I think I figured the answer out on my own.  I have no idea what  I did differently but the GetAttachment function now works and returns the attachment data in the <t:Content> element of the XML response.  That data is a MIME type data string but the problem is that it's encoded in base64 and since I'm running VBScript (which is 32bit) I have to use a base64 decoder.  I found a great one called XStandard Base64 Component.

BUT wait......not done yet.  So the other problem is I'm running this script on a 64bit server and the COM object (or dll file) from the link above is actually 32bit.  So even more fun.....I had to figure out how to run a 32bit COM object on a 64bit server.  Thanks to a helpful link on GFI's Webpage explaining how to do it by making a small registry tweak.  I figured it out but it wasn't the easiest instructions to follow so here are a little easier ones:

1)  Locate your COM object GUID under the HKey_Classes_Root\Wow6432Node\CLSID\[GUID]
      *Easiest way is to search for "XStandard" from the HKey_Classes_Root\Wow6432Node\CLSID\

2)  Once located add a new REG_SZ (string) Value.
      *Name is "AppID" (without quotes) and Data should be COM object GUID from step #1.
      *The GUID needs to include the "{" and "}" at the end.  ex: {118BAD5F-AB34-4140-9C78-0AB786C8A66D}

3)  Add a new key under HKey_Classes_Root\Wow6432Node\AppID\  and call it the same as the COM object GUID.
      *Again, make sure to include the "{" and "}"

4)  Under the new key you just added, add a new REG_SZ (string) Value, and call it "DllSurrogate" and leave the Value empty.

5)  Create a new Key under HKey_Local_Machine\Software\Classes\AppID\
      *Again the new key should be called the same as the COM object’s GUID.  Leave the Value empty.
      *If you get an error saying a key with that name already exists then you can skip this step, that happened to me for some reason.


That's it.  I was able to run the script anad all is well.  :)
0
 
tphelps19Author Commented:
Kind of wished I got a little help though.......
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now