Save mail to msg file using EWS API

后端 未结 8 2163

I\'m using Exchange Web Services Managed API 1.1 to connect to Exchange server 2010 and then find out new emails received. Now I want to save a copy of the .msg file to a fo

8条回答
  •  自闭症患者
    2020-11-28 10:21

    This is how I solved the problem to download from EWS the email message in .eml format via vbs code

    ' This is the function that retrieves the message:
    function CreaMailMsg(ItemId,ChangeKey)
    Dim MailMsg
    Dim GetItemSOAP,GetItemResponse,Content
    
        LogFile.WriteLine (Now() & "-" & ":CreaMailMsg:ID:" & ItemId)
        GetItemSOAP=ReadTemplate("GetItemMsg.xml")
        GetItemSOAP=Replace(GetItemSOAP, "", ItemId)   
        GetItemSOAP=Replace(GetItemSOAP, "", ChangeKey)
        LogFile.WriteLine (Now() & ":GetItemSOAP:" & GetItemSOAP) 
    
        set GetItemResponse=SendSOAP(GetItemSOAP,TARGETURL,"",USERNAME,PASSWORD)
        ' Check we got a Success response
        if not IsResponseSuccess(GetItemResponse, "m:GetItemResponseMessage","ResponseClass") then
            LogFile.WriteLine (Now() & "-" & ":ERRORE:Fallita GetItemMsg:" & GetItemResponse.xml)
            Chiusura 1
        end if
    
    '   LogFile.WriteLine (Now() & "-" & ":DEBUG:riuscita GetItemMsg:" & GetItemResponse.xml)
        Content = GetItemResponse.documentElement.getElementsByTagName("t:MimeContent").Item(0).Text
    '   LogFile.WriteLine (Now() & ":Contenuto MIME" & Content)
    
        CreaMailMsg = WriteAttach2File(Content,"OriginaryMsg.eml")
    
    '   MailMsg.close
        CreaMailMsg = true
    end function
    '###########################################################################
    ' These are the functions the save the message in .eml format
    '###########################################################################
    function WriteAttach2File(Content,nomeAttach)
    Dim oNode,oXML,Base64Decode
        ' Read the contents Base64 encoded and Write a file  
        set oXML=CreateObject("MSXML2.DOMDocument")
        set oNode=oXML.CreateElement("base64")
        oNode.DataType="bin.base64"
        oNode.Text = Content
        Base64Decode = Stream_Binary2String(oNode.nodeTypedValue,nomeAttach)
        Set oNode = Nothing
        Set oXML = Nothing
    end function
    '###########################################################################
    function Stream_Binary2String(binary,nomeAttach)
        Const adTypeText = 2
        Const adTypeBinary = 1
        Dim BinaryStream
    
        Set BinaryStream=CreateObject("ADODB.Stream")
        BinaryStream.Type=adTypeBinary' Binary
        BinaryStream.Open
        BinaryStream.Write binary   
        BinaryStream.Position=0
        BinaryStream.Type=adTypeText
        BinaryStream.CharSet = "us-ascii"
        Stream_Binary2String=BinaryStream.ReadText
        'msgbox Stream_Binary2String
        BinaryStream.SaveToFile ShareName & "\" & nomeAttach,2
    
        Set BinaryStream=Nothing
    end function
    

提交回复
热议问题