Solved

Exchange 2007 Web Services Error with VBScript

Posted on 2012-03-20
2
1,471 Views
Last Modified: 2012-03-22
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
Comment
Question by:tphelps19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
tphelps19 earned 0 total points
ID: 37753284
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
 
LVL 2

Author Closing Comment

by:tphelps19
ID: 37753287
Kind of wished I got a little help though.......
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question