通过Outlook Anywhere使用VBScript发送电子邮件

时间:2011-12-06 22:52:44

标签: email vbscript outlook

我有一个不幸的任务,即通过需要通过VBScript连接到Outlook Anywhere的客户端发送电子邮件。在处理Exchange Server时,我可能无法理解正确的术语,因此答案可能只是因为我不知道我在搜索什么而回避我,而且在Google上工作几小时后我仍然没有找到答案。

我无法使用SMTP,因为脚本将从许多位置/网络连接,它们可能会也可能不会阻止通过默认端口的流量。我无法更改默认端口,因为网络管理员(谁是实际客户)不会更改它。

任何正确方向的指针都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您实际上希望能够在客户端计算机上使用已安装/正在运行的Outlook版本来生成并发送一封电子邮件,该邮件将传递给它正在使用的任何已配置的MTA,可能通过Exchange Server,它在公共网络上配置为在专用LAN或VPN上。如果是这样,你想要看的是使用微软命名法中所谓的“自动化”。对于大多数脚本语言和开发工具,您可以通过COM执行此操作。以下Microsoft文章介绍了如何在VBA中执行此操作,这应该为您提供足够的细节,以便几乎按原样使用VBScript。

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

答案 1 :(得分:0)

我的问题的答案部分在于stackoverflow已经出现在以下问题Ways to send E-Mails over MS Exchange with VBScript

下面的代码(VBA,但足够接近VBScript)只是向Exchange Web服务发送SOAP消息。它是通过网络上的各种零碎(包括上面的链接)构建的。

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