使用EWS API将邮件保存到msg文件

时间:2011-06-09 12:59:00

标签: exchange-server ews-managed-api

我正在使用Exchange Web服务托管API 1.1连接到Exchange Server 2010,然后找到收到的新电子邮件。现在我想将.msg文件的副本保存到磁盘上的文件夹中。

我不想使用任何付费第三方进行整合。

任何帮助将不胜感激。

8 个答案:

答案 0 :(得分:55)

如果您乐意保存为.eml格式,只需使用EWS即可轻松完成,无需第三方库。 .eml文件将包含所有相同的信息,并且可以通过Outlook以与.msg(以及其他程序)相同的方式打开。

message.Load(new PropertySet(ItemSchema.MimeContent));

MimeContent mc = message.MimeContent;
FileStream fs = new FileStream("c:\test.eml", FileMode.Create);

fs.Write(mc.Content, 0, mc.Content.Length);
fs.Close();

清理代码:

message.Load(new PropertySet(ItemSchema.MimeContent));
var mimeContent = message.MimeContent;

using (var fileStream = new FileStream(@"C:\Test.eml", FileMode.Create))
{
    fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
}

答案 1 :(得分:7)

使用EWS的MSG文件没有本机支持。它严格来说是一种Outlook格式。

MSG规范发布于http://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspx。理解它有点复杂,但可行。您需要下拉消息的所有属性,然后将其序列化为OLE结构化文件格式。这不是一件容易的事。

最后,你可能最好选择第三方图书馆,否则这可能是一件大事。

答案 2 :(得分:2)

这个建议是@mack发表的评论,但我认为它应该得到自己的答案,如果除了格式和答案与评论的可读性之外别无他法。

using (FileStream fileStream = 
    File.Open(@"C:\message.eml", FileMode.Create, FileAccess.Write)) 
{ 
    message.Load(new PropertySet(ItemSchema.MimeContent)); 
    MimeContent mc = message.MimeContent; 
    fileStream.Write(mc.Content, 0, mc.Content.Length); 
}

答案 3 :(得分:1)

如果eml格式是一个选项而php是语言,则在保存文件之前在Mimencontent上使用base64_decode。

如果使用https://github.com/Heartspring/Exchange-Web-Services-for-PHPhttps://github.com/hatsuseno/Exchange-Web-Services-for-PHP需要添加

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

在第245或247行。

答案 4 :(得分:1)

您可以通过message.MimeContent轻松访问邮件的MIME内容,并将邮件另存为EML文件。最新的(2013年和2016年)版本的Outlook将能够直接打开EML文件。

message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mimcon = message.MimeContent;
FileStream fStream = new FileStream("c:\test.eml", FileMode.Create);
fStream.Write(mimcon.Content, 0, mimcon.Content.Length);
fStream.Close();

如果您仍需要转换为MSG格式,您可以选择以下几种方法:

1)记录MSG文件格式 - 它是一个OLE存储(IStorage)文件。见https://msdn.microsoft.com/en-us/library/cc463912(v=exchg.80).aspx

2)使用第三方MSG文件包装器,例如Independentsoft中的一个:http://www.independentsoft.de/msg/index.html。设置Outlook期望的所有属性都具有挑战性。

3)使用Redemption直接将EML文件转换为MSG:

set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.CreateMessageFromMsgFile("c:\test.msg")
Msg.Import("c:\test.eml", 1024)
Msg.Save

答案 5 :(得分:0)

您可以使用EWS API和C#下载所有附件。以下是给出的示例:

byte[][] btAttachments = new byte[3][]; //To store  3 attachment 

if (item.HasAttachments) {
    EmailMessage message = EmailMessage.Bind(objService, new ItemId(item.Id.UniqueId.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

    noOfAttachment = message.Attachments.Count;

    // Iterate through the attachments collection and load each attachment.
    foreach(Attachment attachment in message.Attachments)
    {
        if (attachment is FileAttachment)
        {
            FileAttachment fileAttachment = attachment as FileAttachment;
            // Load the file attachment into memory and print out its file name.
            fileAttachment.Load();
            //Get the Attachment as bytes
            if (i < 3) {
                btAttachments[i] = fileAttachment.Content;
                i++;
            }
        }
        // Attachment is an item attachment.
                    else
        {
            // Load attachment into memory and write out the subject.
            ItemAttachment itemAttachment = attachment as ItemAttachment;
            itemAttachment.Load(new PropertySet(EmailMessageSchema.MimeContent));
            MimeContent mc = itemAttachment.Item.MimeContent;
            if (i < 3) {

                btAttachments[i] = mc.Content;
                i++;
            }
        }
    }
}

上面的代码将所有附件转换为字节。一旦有字节,就可以将字节转换为所需的格式。 要将字节转换为文件并保存在磁盘中,请遵循以下链接: Write bytes to file http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-byte-array-to-file.html

答案 6 :(得分:0)

这就是我通过vbs代码从EWS下载.eml格式的电子邮件的问题

' 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

答案 7 :(得分:0)

如果您从Outlook的EntryID通过VSTO(Hex)转到EwsID,您需要查看此处:http://bernhardelbl.wordpress.com/2013/04/15/converting-entryid-to-ewsid-using-exchange-web-services-ews/

救了我。我不断得到“数据已损坏”。消息。

相关问题