2011-06-09 38 views
24

Tôi đang sử dụng API được quản lý dịch vụ Web Exchange 1.1 để kết nối với máy chủ Exchange 2010 và sau đó tìm ra email mới nhận được. Bây giờ tôi muốn lưu một bản sao của tập tin .msg vào một thư mục trên đĩa.Lưu thư vào tệp tin msg bằng EWS API

Tôi không muốn sử dụng bất kỳ bên thứ ba trả phí nào để tích hợp.

Mọi trợ giúp sẽ được đánh giá cao.

+0

bạn đã cố gắng tiết kiệm để eml *** *** hoặc *** msg *** nộp..? – Kiquenet

Trả lời

6

Không có hỗ trợ gốc cho tệp MSG sử dụng EWS. Đó là định dạng của Outlook.

Thông số MSG được xuất bản tại http://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspx. Đó là một chút phức tạp để hiểu, nhưng có thể. Bạn sẽ cần phải kéo xuống tất cả các thuộc tính cho tin nhắn và sau đó tuần tự hóa nó thành một định dạng tệp có cấu trúc OLE. Nó không phải là một nhiệm vụ dễ dàng.

Cuối cùng, bạn có thể nên sử dụng thư viện của bên thứ ba nếu không, đó có thể là một nhiệm vụ lớn để thực hiện.

47

Nếu bạn vui mừng lưu vào định dạng .eml thay vào đó, nó có thể được thực hiện rất dễ dàng chỉ bằng EWS và không có thư viện của bên thứ ba. Các tập tin .eml sẽ chứa tất cả các thông tin tương tự và có thể được mở bởi Outlook trong cùng một cách như .msg (và cũng bởi các chương trình khác).

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(); 

Làm sạch mã:

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); 
} 
+0

Tôi biết điều này đã qua rồi, nhưng cảm ơn câu trả lời của bạn. Đã cứu tôi rất nhiều thời gian. – Fox

+3

Đồng ý. Điều này rất hữu ích! Tôi chỉ thay đổi nó một chút để sử dụng một tuyên bố sử dụng: sử dụng (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); } – mack

+0

Tôi đã dọn sạch mã bằng cách bao gồm các câu lệnh 'using', và cũng sửa lỗi với chuỗi ký tự (' @ "C: \ Test.eml" '). –

0

Bạn có thể tải tất cả các file đính kèm sử dụng EWS API và C#. Dưới đây là ví dụ đưa ra:

   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++; 
          } 
         } 
        } 

}

Trên đang chuyển đổi tất cả các tập tin đính kèm vào byte. Khi bạn có byte, bạn có thể chuyển đổi byte thành định dạng yêu cầu của mình. Để Chuyển đổi byte vào các tập tin và lưu trong đĩa theo các liên kết dưới đây: Write bytes to file http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-byte-array-to-file.html

2

đề nghị này đã được đăng dưới dạng một comment bởi @mack, nhưng tôi nghĩ rằng nó xứng đáng vị trí của mình như một câu trả lời, nếu không có lý do khác ngoài định dạng và khả năng đọc câu trả lời so với nhận xét.

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); 
} 
0

Đây là cách tôi giải quyết được vấn đề để tải về từ EWS thông báo email ở định dạng .eml qua mã vbs

' 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 
1

Bạn có thể dễ dàng truy cập nội dung MIME của tin nhắn qua tin nhắn.MimeContent và lưu tin nhắn dưới dạng tệp EML. Phiên bản mới nhất (2013 và 2016) của Outlook sẽ có thể mở tệp EML trực tiếp.

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(); 

Nếu bạn vẫn cần phải chuyển đổi sang định dạng MSG, bạn có một vài lựa chọn:

1) định dạng tập tin MSG được ghi chép lại - đó là một OLE cửa hàng (IStorage) tập tin. Xem https://msdn.microsoft.com/en-us/library/cc463912(v=exchg.80).aspx

2) Sử dụng trình bao bọc tệp MSG của bên thứ ba, chẳng hạn như trình bao bọc từ Independentsoft: http://www.independentsoft.de/msg/index.html. Đặt tất cả các thuộc tính mà Outlook mong đợi có thể là một thách thức.

3) Chuyển đổi tập tin EML để MSG trực tiếp sử dụng Redemption:

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

Lưu vào ***. Eml *** thật dễ dàng, nhưng lưu thành ***. Msg *** yêu cầu ** bên thứ ba ** (_NOT free_) – Kiquenet

+0

Bạn vẫn có thể làm việc trực tiếp với tệp MSG ở cấp nhị phân , nhưng câu hỏi đặt ra là liệu làm như vậy sẽ giúp bạn tiết kiệm thời gian hay tiền bạc - có lẽ là không. –

Các vấn đề liên quan