使用 C# 从 Lotus Notes 电子邮件中提取/导出附件

发布于 2024-08-04 17:49:23 字数 1198 浏览 7 评论 0原文

我需要将 Lotus Notes 电子邮件附件提取/导出到文件系统中。为此,我编写了以下方法,但每次我在 foreach 行收到错误(项目中的 NotesItem nItem)..任何人都可以告诉我我做错了什么..

谢谢 贾瓦林

    public void GetAttachments()
    {
        NotesSession session = new NotesSession();
        //NotesDocument notesDoc = new NotesDocument();
        session.Initialize("");

        NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
        NotesView inbox = NotesDb.GetView("By _Author");
        NotesDocument docInbox = inbox.GetFirstDocument();
        object[] items = (object[])docInbox.Items;
        **foreach (NotesItem nItem in items)**
        {

            //NotesItem nItem = (NotesItem)o1;
            if (nItem.Name == "$FILE")
            {
                NotesItem file = docInbox.GetFirstItem("$File");
                string fileName = ((object[])nItem.Values)[0].ToString();
                NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.GetAttachment(fileName);

                if (attachfile != null)
                {
                    attachfile.ExtractFile("C:\\temps\\export\\" + fileName);
                }
            }
        }

I need to extract/export the lotus notes email attachment into file system. for that I wrote following method but each time I am receiving an error at line foreach (NotesItem nItem in items).. Can anybody please tell me what I am doing wrong ..

Thanks
Jwalin

    public void GetAttachments()
    {
        NotesSession session = new NotesSession();
        //NotesDocument notesDoc = new NotesDocument();
        session.Initialize("");

        NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
        NotesView inbox = NotesDb.GetView("By _Author");
        NotesDocument docInbox = inbox.GetFirstDocument();
        object[] items = (object[])docInbox.Items;
        **foreach (NotesItem nItem in items)**
        {

            //NotesItem nItem = (NotesItem)o1;
            if (nItem.Name == "$FILE")
            {
                NotesItem file = docInbox.GetFirstItem("$File");
                string fileName = ((object[])nItem.Values)[0].ToString();
                NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.GetAttachment(fileName);

                if (attachfile != null)
                {
                    attachfile.ExtractFile("C:\\temps\\export\\" + fileName);
                }
            }
        }

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

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

发布评论

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

评论(2

挽心 2024-08-11 17:49:23

您不需要使用 $File 项来获取附件名称。相反,您可以使用 NotesDocument 类的 HasEmbedded 和 EmbeddedObject 属性。

public void GetAttachments()
    {
    NotesSession session = new NotesSession();
    //NotesDocument notesDoc = new NotesDocument();
    session.Initialize("");

    NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
    NotesView inbox = NotesDb.GetView("By _Author");
    NotesDocument docInbox = inbox.GetFirstDocument();

    // Check if any attachments
    if (docInbox.hasEmbedded)
    {
        NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.embeddedObjects[0];

        if (attachfile != null)
        {
            attachfile.ExtractFile("C:\\temps\\export\\" + attachfile.name);
        }
    }

You don't need to use the $File item to get the attachment name(s). Rather, you can use the HasEmbedded and EmbeddedObject properties of the NotesDocument class.

public void GetAttachments()
    {
    NotesSession session = new NotesSession();
    //NotesDocument notesDoc = new NotesDocument();
    session.Initialize("");

    NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
    NotesView inbox = NotesDb.GetView("By _Author");
    NotesDocument docInbox = inbox.GetFirstDocument();

    // Check if any attachments
    if (docInbox.hasEmbedded)
    {
        NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.embeddedObjects[0];

        if (attachfile != null)
        {
            attachfile.ExtractFile("C:\\temps\\export\\" + attachfile.name);
        }
    }
得不到的就毁灭 2024-08-11 17:49:23

埃德的解决方案对我不起作用。即使 HasEmbedded 标志为 true,我的 Notes 数据库设计似乎也将 EmbeddedObjects 属性保留为空。所以我结合了 Ed 和 Jwalin 的解决方案,修改它们以从 Notes 文档中获取所有附件。

        NotesDocument doc = viewItems.GetNthEntry(rowCount).Document;
        if (doc.HasEmbedded)
        {
           object[] items = (object[])doc.Items;
           foreach (NotesItem item in items)
           {
              if(item.Name.Equals("$FILE"))
              {
                 object[] values = (object[])item.Values;
                 doc.GetAttachment(values[0].ToString()).ExtractFile(fileSavePath + values[0].ToString());
              }
           }

Ed's solution didn't work for me. Something about my Notes db design seems to have left the EmbeddedObjects property null even when the HasEmbedded flag is true. So I sort of combined the Ed's and Jwalin's solutions, modifying them to fetch all attachments from a Notes document.

        NotesDocument doc = viewItems.GetNthEntry(rowCount).Document;
        if (doc.HasEmbedded)
        {
           object[] items = (object[])doc.Items;
           foreach (NotesItem item in items)
           {
              if(item.Name.Equals("$FILE"))
              {
                 object[] values = (object[])item.Values;
                 doc.GetAttachment(values[0].ToString()).ExtractFile(fileSavePath + values[0].ToString());
              }
           }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文