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

发布于 2024-12-20 09:15:21 字数 2131 浏览 5 评论 0原文

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

下面的代码显示了我如何导出附件。您能建议我如何修改它以保存电子邮件吗? 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);
        }
      }
    }
  }
}

I need to export (save to) hard drive my Lotus Notes emails.
I figured out the way how to save attachments to HDD, but I can't figure out the way of how to save the whole email.

The code below shows how I export attachments. Can you suggest how can I modify it to save emails?
PS- I am new to programming.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-12-27 09:15:21

Bob Babalan 的这篇文章解释了如何使用导出 Lotus 文档爪哇。同样的原理也适用于 C# 或 VB。该文档被转换为 MIME 并写入磁盘。

或者在版本 8.5.3 中(我认为它是从 8.5.1 开始的),您可以将其从邮件文件拖放到文件系统中。

This post by Bob Babalan explains how to export lotus documents using Java. The same principle should work in C# or VB. The document is cnverted into MIME and written to the disk.

Or in version 8.5.3 (I think it started witn 8.5.1) you can just drag and drop it from the mail file to the file system.

暮凉 2024-12-27 09:15:21

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

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 文件保存到给定路径。

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

I know it is a bit late, but this is, what I did. (Based on Bob Babalan)
Bobs Solution helped me alot to understand NotesMIMEEntities, but in his solution, he only traversed the MIME-Tree to the second "layer". This will traverse multiple layers.

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

I would call this methods like this, to save the EML-File to a given path.

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文