Save mail to msg file using EWS API

2019-01-06 15:37发布

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 folder on the disk.

I do not want to use any paid third party to integrate.

Any help will be appreciated.

8条回答
再贱就再见
2楼-- · 2019-01-06 16:07

If eml format is an option and php is the language use base64_decode on the Mimencontent before save on file.

If using https://github.com/Heartspring/Exchange-Web-Services-for-PHP or https://github.com/hatsuseno/Exchange-Web-Services-for-PHP need to add

 $newmessage->mc = $messageobj->MimeContent->_;

on line 245 or 247.

查看更多
Deceive 欺骗
3楼-- · 2019-01-06 16:10

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-->", ItemId)   
    GetItemSOAP=Replace(GetItemSOAP, "<!--ITEMCHANGEKEY-->", 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
查看更多
登录 后发表回答