views:

1483

answers:

3

Hello

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);
                }
            }
        }
+1  A: 

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);
        }
    }
Ed Schembor
A: 

I am getting some COM error on Session.Initialize("pass") error while trying this.HELP!!!!

Kap
A: 

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