Search code examples
c#emaillotus

Add attachments to Lotus Notes mail by C#


I found answer about how to send Lotus Notes Mail by C# at this website

    public void ComposeMemo(String sendto, String subject, String body)
    {
        // instantiate a Notes session and workspace
        Type NotesSession = Type.GetTypeFromProgID("Notes.NotesSession");
        Type NotesUIWorkspace = Type.GetTypeFromProgID("Notes.NotesUIWorkspace");
        Object sess = Activator.CreateInstance(NotesSession);
        Object ws = Activator.CreateInstance(NotesUIWorkspace);

        // open current user's mail file
        String mailServer = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailServer", true });
        String mailFile = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailFile", true });
        NotesUIWorkspace.InvokeMember("OpenDatabase", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile });
        Object uidb = NotesUIWorkspace.InvokeMember("GetCurrentDatabase", BindingFlags.InvokeMethod, null, ws, null);
        Object db = NotesUIWorkspace.InvokeMember("Database", BindingFlags.GetProperty, null, uidb, null);
        Type NotesDatabase = db.GetType();

        // compose a new memo
        Object uidoc = NotesUIWorkspace.InvokeMember("ComposeDocument", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile, "Memo", 0, 0, true });
        Type NotesUIDocument = uidoc.GetType();
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "EnterSendTo", sendto });
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Subject", subject });
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body", body });

        // bring the Notes window to the front
        String windowTitle = (String)NotesUIDocument.InvokeMember("WindowTitle", BindingFlags.GetProperty, null, uidoc, null);
        Interaction.AppActivate(windowTitle);
    }

How to add attachments to Lotus Notes mail?? Thank you for providing any requested information.

This code I had tried is work! just add five lines.

    public void ComposeMemo(String sendto, String subject, String body)
    {
        // instantiate a Notes session and workspace
        Type NotesSession = Type.GetTypeFromProgID("Notes.NotesSession");
        Type NotesUIWorkspace = Type.GetTypeFromProgID("Notes.NotesUIWorkspace");
        Object sess = Activator.CreateInstance(NotesSession);
        Object ws = Activator.CreateInstance(NotesUIWorkspace);

        // open current user's mail file
        String mailServer = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailServer", true });
        String mailFile = (String)NotesSession.InvokeMember("GetEnvironmentString", BindingFlags.InvokeMethod, null, sess, new Object[] { "MailFile", true });
        NotesUIWorkspace.InvokeMember("OpenDatabase", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile });
        Object uidb = NotesUIWorkspace.InvokeMember("GetCurrentDatabase", BindingFlags.InvokeMethod, null, ws, null);
        Object db = NotesUIWorkspace.InvokeMember("Database", BindingFlags.GetProperty, null, uidb, null);
        Type NotesDatabase = db.GetType();

        // compose a new memo
        Object uidoc = NotesUIWorkspace.InvokeMember("ComposeDocument", BindingFlags.InvokeMethod, null, ws, new Object[] { mailServer, mailFile, "Memo", 0, 0, true });
        Type NotesUIDocument = uidoc.GetType();
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "EnterSendTo", sendto });
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Subject", subject });
        NotesUIDocument.InvokeMember("FieldSetText", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body", body });


        StringCollection paths = new StringCollection();
        paths.Add(@"C:\Users\Public\Pictures\Sample Pictures\IMG_1066.JPG");
        Clipboard.SetFileDropList(paths);

        NotesUIDocument.InvokeMember("GotoField", BindingFlags.InvokeMethod, null, uidoc, new Object[] { "Body" });
        NotesUIDocument.InvokeMember("Paste", BindingFlags.InvokeMethod, null, uidoc, null);



        // bring the Notes window to the front
        String windowTitle = (String)NotesUIDocument.InvokeMember("WindowTitle", BindingFlags.GetProperty, null, uidoc, null);
        Interaction.AppActivate(windowTitle);
    }

Solution

  • There is some information here that might help you.

    There are pointers for adding an attachment to a memo, but the same class (NotesUIDocument) seems to be used.

    [from source]

    Workaround #1: Copy the file to the clipboard, then use NotesUIDocument.Paste. Not my favorite because it obviously destroys anything the user had on the clipboard, but this is pretty easy to do.

    Workaround #2: Create the email using only back-end classes, then display it to the user using the NotesUIWorkspace.EditDocument method. I notice puru1981 mentioned using NotesRichTextItem and NotesEmbeddedObject, but I find using richtext a bit clunky. Instead, I'm using the newer mime classes (NotesMIMEEntity for example). I don't have any examples using OLE or C#, but here's a COM/VBA solution that you can use to get started.

    http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/Lotus_SmartSuite/Lotus_Notes/Q_24468076.html#24575544

    A couple of things to keep in mind:

    1) I cannot think of a situation where you would need to call CreatObject. That is a LotusScript/VB command that should be reserved for extending the client from within.

    2) Only call CreateInstance for NotesSession and NotesUIWorkspace. Those are the root objects in Notes land. NotesSession for back-end access, NotesUIWorkspace for GUI access. All other handles should be obtained from one of these objects or a child thereof.