Extract / export applications from Lotus Notes Email using C #

I need to extract / export a Notes Lotus Notes email attachment to the file system. I wrote the following method for this, but every time I get an error in the foreach line (NotesItem nItem in items). Can someone 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);
                }
            }
        }

      

+2


source to share


2 answers


You don't need to use the $ File element to get the attachment names. 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);
        }
    }

      

+2


source


Ed's solution didn't work for me. Something about my Notes db project seems to have left the EmbeddedObjects property null even though the HasEmbedded flag is true. So I kind of combined Ed and Jwalin's solutions, modifying them to extract all attachments from the 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());
              }
           }

      

0


source







All Articles