0

I have the unfortunate task of sending e-mail through a client that needs to connect to Outlook Anywhere through VBScript. I may not understand the correct terminology when dealing with Exchange Server, therefore the answer may be evading me simply because I don't know what I'm searching for, and after hours on Google I still haven't found the answer.

I cannot use SMTP, since the script will be connecting from many locations/networks and they may or may not block the traffic through the default port. I cannot change the default port because the network admin (who is the actual customer) will not change it.

Any pointers in the right direction will be appreciated.

Chris
  • 439
  • 7
  • 15

2 Answers2

1

If I understand your question correctly, you actually want to be able to use the installed/running version of Outlook on a client machine to generate and send an email message which it will hand off to whatever configured MTA it is using, probably via the Exchange Server it is configured to on a private LAN or over VPN when on a public network. If so, what you want to look at is using what is called "Automation" in Microsoft's nomenclature. For most scripting languages and development tools, you can do this via COM. The following Microsoft article shows how to do this in VBA which should give you enough specifics to use almost as-is for VBScript.

http://support.microsoft.com/kb/209948

dmarietta
  • 1,940
  • 3
  • 25
  • 32
  • Not quite, the client has a company server with a dedicated e-mail address that they wish to send a message from. Would automation not simply send the e-mail from the client's default account? – Chris Dec 06 '11 at 23:55
  • Automation to Outlook will cause the email to be sent via Outlook's configured profile. If the profile is configured to work sending email in the fashion you want it to appear to be sent from, then that is what you should get. So if your profile was configured for Exchange, it would be sent via the Exchange server. If it was configured to use some other SMTP server, then it will be sent using that. Whatever is in the profile, will control how Outlook attempts to deliver it. Hope that clarifies for you... – dmarietta Dec 07 '11 at 01:21
  • Yes thanks, but that is not what I need to do. The customer is one of those loosely connected franchise like entities, and the ultimate client machine will be registered with someone's personal e-mail account. The mail I send must go through the 'official' account, so simply letting Outlook decide won't cut it. – Chris Dec 07 '11 at 14:30
  • Then I would suggest you need to clarify your question with all the exact details of what you are trying to accomplish. You specifically asked how to do it through Outlook in your original question. To get better direction, you probably need to clarify the general organization of the network as it relates to email. I.E. in your last response you say it must use the "official account", but yet I see no detail explaining what exactly you mean by that. Hopefully that will help you form a clearer question that will give you the direction you need. Hope that helps... – dmarietta Dec 07 '11 at 21:56
  • Thanks again for your replies and help, it is appreciated. I knew my question wasn't clear enough but I did specifically mention Outlook Anywhere and needing to use VBScript to send through Exchange Server. I have however found the answer and will post it soon. – Chris Dec 08 '11 at 14:41
0

The answer to my question is partially on stackoverflow already at the following question Ways to send E-Mails over MS Exchange with VBScript.

The code below (VBA, but close enough to VBScript) is simply sending a SOAP message to the Exchange Web Service. It was built from various bits and pieces found all over the web (including the links above).

Option Explicit

' ---------------------------------------------------------
' CONFIGURATION - change as needed
' ---------------------------------------------------------
Const TARGETURL = "https://mail.XXXXX.com/ews/exchange.asmx"
Const USERNAME = "XXXXX\dnreply"
Const PASSWORD = "X1X2X3X4X!x@x#x$x%"

Sub SendMessageEWS()
    Dim SOAP
    SOAP = CreateMessageSOAP()

    ' Send the SOAP request, and return the response
    Dim oXMLHTTP, oXml

    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    Set oXml = CreateObject("MSXML2.DOMDocument")

    ' Send the request
    oXMLHTTP.Open "POST", TARGETURL, False, USERNAME, PASSWORD
    oXMLHTTP.setRequestHeader "Content-Type", "text/xml"
    oXMLHTTP.send SOAP

    If oXMLHTTP.Status = "200" Then
        ' Get response
        If oXml.LoadXML(oXMLHTTP.ResponseText) Then
            ' Success
            Debug.Print oXml.XML
        End If
    Else
        Debug.Print oXMLHTTP.ResponseText
        MsgBox "Response status: " & oXMLHTTP.Status
    End If

End Sub

Function CreateMessageSOAP()
    ' Normally this is done by using the DOM, but this is easier for a demo...
    Dim SOAPMsg

    SOAPMsg = SOAPMsg & "<?xml version='1.0' encoding='utf-8'?>"
    SOAPMsg = SOAPMsg & " <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
    SOAPMsg = SOAPMsg & "   <soap:Body>"
    SOAPMsg = SOAPMsg & "     <CreateItem MessageDisposition='SendAndSaveCopy' xmlns='http://schemas.microsoft.com/exchange/services/2006/messages'>"
    SOAPMsg = SOAPMsg & "         <SavedItemFolderId>"
    SOAPMsg = SOAPMsg & "             <t:DistinguishedFolderId Id='sentitems' />"
    SOAPMsg = SOAPMsg & "         </SavedItemFolderId>"
    SOAPMsg = SOAPMsg & "         <Items>"
    SOAPMsg = SOAPMsg & "             <t:Message>"
    SOAPMsg = SOAPMsg & "                 <t:Subject>Exchange Web Service E-Mail Test</t:Subject>"
' For HTML message body
    SOAPMsg = SOAPMsg & "                 <t:Body BodyType='HTML'><![CDATA[<h1>Test html body</h1>]]></t:Body>"
' For text message body
'    SOAPMsg = SOAPMsg & "                 <t:Body BodyType='Text'><![CDATA[Test text body]]></t:Body>"
    SOAPMsg = SOAPMsg & "                 <t:ToRecipients>"
    SOAPMsg = SOAPMsg & "                     <t:Mailbox>"
    SOAPMsg = SOAPMsg & "                         <t:EmailAddress>recipient@XXXXX.com</t:EmailAddress>"
    SOAPMsg = SOAPMsg & "                     </t:Mailbox>"
    SOAPMsg = SOAPMsg & "                 </t:ToRecipients>"
    SOAPMsg = SOAPMsg & "             </t:Message>"
    SOAPMsg = SOAPMsg & "         </Items>"
    SOAPMsg = SOAPMsg & "     </CreateItem>"
    SOAPMsg = SOAPMsg & "   </soap:Body>"
    SOAPMsg = SOAPMsg & " </soap:Envelope>"

    CreateMessageSOAP = SOAPMsg
End Function
Community
  • 1
  • 1
Chris
  • 439
  • 7
  • 15
  • Have you tried using OAuth instead of Basic Authentication to connect to the EWS exzchange URL where it can be connected using TokenID & ClientID as extra parameters. – Geethu Mar 25 '21 at 08:48
  • This was from 2011, I can't even remember what the use case was. So sorry but I'm not able to assist... – Chris Mar 25 '21 at 14:39