首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Lotus Notes中使用C#将电子邮件保存为eml

在Lotus Notes中使用C#将电子邮件保存为eml
EN

Stack Overflow用户
提问于 2011-12-10 06:24:50
回答 2查看 7.3K关注 0票数 3

我需要导出(保存到)硬盘上的Lotus Notes电子邮件。我想出了如何将附件保存到HDD的方法,但我想不出如何保存整个电子邮件的方法。

下面的代码显示了我如何导出附件。你能建议我如何修改它来保存电子邮件吗?PS-我是编程新手。

代码语言:javascript
复制
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);
        }
      }
    }
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-10 16:41:45

Bob BabalanThis帖子解释了如何使用Bob Babalan导出lotus文档。同样的原理应该适用于C#或VB。文档被转换为MIME并写入磁盘。

或者在8.5.3版本中(我想它是从8.5.1开始的),你可以把它从邮件文件拖放到文件系统中。

票数 2
EN

Stack Overflow用户

发布于 2016-03-24 18:02:33

我知道有点晚了,但这就是我所做的。(基于Bob Babalan) Bobs解决方案帮助我理解了很多NotesMIMEEntities,但在他的解决方案中,他只遍历了MIME树到第二层。这将遍历多个层。

代码语言:javascript
复制
public static 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-File保存到给定的路径。

代码语言:javascript
复制
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)
        GetMIME(writer, mimeEntity);
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8452468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档