2011-12-09 116 views
3

我需要將我的Lotus Notes電子郵件導出(保存到)硬盤。 我想出瞭如何將附件保存到硬盤,但我無法弄清楚如何保存整個電子郵件的方式。Lotus Notes - 保存整個電子郵件eml c#

下面的代碼顯示了我如何導出附件。你能建議我如何修改它以保存電子郵件? PS-我是編程新手。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Domino; 
using System.Collections; 

namespace ExportLotusAttachments 
{ 
    class Class1 
    { 
    public void ScanForEmails() 
    { 
     String textBox1 = "c:\\1"; 
     NotesSession session = new NotesSession(); 
     session.Initialize(""); 
     NotesDbDirectory dir = null; 
     dir = session.GetDbDirectory(""); 
     NotesDatabase db = null; 
     db = dir.OpenMailDatabase(); 
     NotesDatabase NDb = dir.OpenMailDatabase(); //Database connection 

     //ArrayList that will hold names of the folders 
     ArrayList LotusViews2 = new ArrayList(); 

     foreach (NotesView V in NDb.Views) 
     { 
     if (V.IsFolder && !(V.Name.Equals("($All)"))) 
     { 
      NotesView getS = V; 
      LotusViews2.Add(getS.Name); 
     } 
     } 

     foreach (String obj in LotusViews2) 
     { 
     NotesDocument NDoc; 
     NotesView nInboxDocs = NDb.GetView(obj); 
     NDoc = nInboxDocs.GetFirstDocument(); 
     String pAttachment; 

     while (NDoc != null) 
     { 
      if (NDoc.HasEmbedded && NDoc.HasItem("$File")) 
      { 
      object[] AllDocItems = (object[])NDoc.Items; 
      foreach (object CurItem in AllDocItems) 
      { 
       NotesItem nItem = (NotesItem)CurItem; 
       if (IT_TYPE.ATTACHMENT == nItem.type) 
       { 
       String path = textBox1; 
       pAttachment = ((object[])nItem.Values)[0].ToString(); 

       if (!System.IO.Directory.Exists(path)) 
       { 
        System.IO.Directory.CreateDirectory(textBox1); 
       } 

       try 
       { 
        NDoc.GetAttachment(pAttachment).ExtractFile(@path + pAttachment); 
       } 
       catch { } 
       } 
      } 
      } 
      NDoc = nInboxDocs.GetNextDocument(NDoc); 
     } 
     } 
    } 
    } 
} 
+0

請在發佈之前預覽您的代碼(您可以通過查看下面輸入的區域來完成此操作)進行格式設置。它不僅使您的問題更具可讀性,而且節省時間,因爲其他人不必花費時間修復它。 :)人們閱讀和理解越容易,你就越有可能得到答案。謝謝。 –

回答

3

This後由Bob Babalan介紹瞭如何使用Java出口的Lotus文檔。同樣的原則應該在C#或VB中工作。該文件被轉換成MIME並寫入磁盤。

或者在8.5.3版本中(我認爲它開始於8.5.1),您可以將其從郵件文件拖放到文件系統。

+1

不幸的是,拖放到.eml功能不會通過API公開。 – leyrer

+0

那麼,絕對沒有辦法讓電子郵件存入硬盤? – Andrew

+0

當然有,但它可能需要一些如上所述的編程。結果將是一個帶有電子郵件的html文件。 –

0

我知道這有點遲,但是這是,我做了什麼。 (根據Bob Babalan) Bobs Solution幫助我理解了NotesMIMEEntities,但是在他的解決方案中,他僅將MIME-Tree移動到第二個「層」。這將遍歷多個層次。

public void GetMIME(StreamWriter writer, NotesMIMEEntity mimeEntity) 
{ 
    try 
    { 
     string contentType = null; 
     string headers = null; 
     string content = null; 
     string preamble = null; 
     MIME_ENCODING encoding; 

     contentType = mimeEntity.ContentType; 
     headers = mimeEntity.Headers; 
     encoding = mimeEntity.Encoding; 

     // message envelope. If no MIME-Version header, add one 
     if (!headers.Contains("MIME-Version:")) 
      writer.WriteLine("MIME-Version: 1.0"); 
     writer.WriteLine(headers); 

     // for multipart, usually no main-msg content... 
     content = mimeEntity.ContentAsText; 
     if (content != null && content.Trim().Length > 0) 
      writer.WriteLine(content); 
     writer.Flush(); 

     if (contentType.StartsWith("multipart")) 
     { 
      preamble = mimeEntity.Preamble; 
      NotesMIMEEntity mimeChild = mimeEntity.GetFirstChildEntity(); 

      while (mimeChild != null) 
      { 
       GetMimeChild(writer, mimeChild); 
       mimeChild = mimeChild.GetNextSibling(); 
      } 
     } 

     writer.WriteLine(mimeEntity.BoundaryEnd); 
     writer.Flush(); 
    } 
    catch (Exception ex) 
    { 
     Logging.Log(ex.ToString()); 
    } 
} 

private void GetMimeChild(StreamWriter writer, NotesMIMEEntity mimeEntity) 
{ 
    string contentType = null; 
    string headers = null; 
    string content = null; 
    string preamble = null; 
    MIME_ENCODING encoding; 

    contentType = mimeEntity.ContentType; 
    headers = mimeEntity.Headers; 
    encoding = mimeEntity.Encoding; 

    if (encoding == MIME_ENCODING.ENC_IDENTITY_BINARY) 
    { 
     mimeEntity.EncodeContent(MIME_ENCODING.ENC_BASE64); 
     headers = mimeEntity.Headers; 
    } 

    preamble = mimeEntity.Preamble; 
    writer.Write(mimeEntity.BoundaryStart); 

    if (!content.EndsWith("\n")) 
     writer.WriteLine(""); 

    writer.WriteLine(headers); 
    writer.WriteLine(); 

    writer.Write(mimeEntity.ContentAsText); 

    if (contentType.StartsWith("multipart")) 
    { 
     preamble = mimeEntity.Preamble; 
     NotesMIMEEntity mimeChild = mimeEntity.GetFirstChildEntity(); 

     while (mimeChild != null) 
     { 
      GetMimeChild(writer, mimeChild); 
      mimeChild = mimeChild.GetNextSibling(); 
     } 
    } 

    writer.Write(mimeEntity.BoundaryEnd); 
    writer.Flush(); 
} 

我會調用這樣的方法來將EML文件保存到給定的路徑。

using (FileStream fs = new FileStream (path,FileMode.Create,FileAccess.ReadWrite,FileShare.None)) 
{ 
    using (StreamWriter writer = new StreamWriter(fs)) 
    { 
    NotesMimeEntity mimeEntity = notesDocument.GetMIMEEntity(); 
    if (mimeEntity != null) 
     email.GetMIME(writer, mimeEntity); 
    } 
} 
相關問題