• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2128
  • Last Modified:

Reading Lotus Notes mailbox from C#

We have a .NET windows application written in C#. We have a mail account set up in "Lotus Notes 6.5" for emails sent to our customer service department. I want to get the C# application to "read" the email that arrives in this mail box.

The reason is that we have some jobs that we wish to automate. For example, a customer might request us to resend a quote. In this instance the C# application will read the request and resend the quote, rather than having a customer service representative do it manually.
Thanks in advance.
  • 2
  • 2
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
can you use POP3 to access your mailbox? I use http://www.lesnikowski.com/mail/Index.aspx to retreive emails from Exchange.
No problem.

The first step is to make sure that Notes is installed on the computer that is running the code.

Next, add a reference to the "Lotus Domino Objects" library (on the COM tab).

Now, include the Domino reference in your source file:

    using Domino;

The only thing left is to read the records from the mail file.  Here's a basic example to get you started.  This example simply reads the mail found in the inbox folder.  If you need a more complex query, let me know.  You'll also want to change the mailServer and mailFile variables to point to your customer service email file rather than the current user's file.

            NotesSession sess = new NotesSession();
            String mailServer = sess.GetEnvironmentString("MailServer", true);
            String mailFile = sess.GetEnvironmentString("MailFile", true);
            NotesDatabase db = sess.GetDatabase(mailServer, mailFile, false);
            NotesView inbox = db.GetView("($Inbox)");
            NotesDocument doc = inbox.GetFirstDocument();
            while (doc != null)
                String subject = (string)((object[])doc.GetItemValue("Subject"))[0];
                String from = (string)((object[])doc.GetItemValue("From"))[0];
                String body = doc.GetFirstItem("Body").Text;
                doc = inbox.GetNextDocument(doc);

Open in new window

stanfordaAuthor Commented:
Thanks for the very quick responses. I had a couple of issues getting the client running on my dev machine as I normally run Notes in Citrix, but got the client working OK in the end.
 Just a couple of things.;
- I initially couldn't get the Session to start... then I realised I hadn't set the password...doh. Sorted that, so no worries.
- sess.GetDatabase always returned NULL no matter what I put into the Server and FileName. I went into the Notes Client and looked at the database properties and used the sames settings in the call... still no joy. Had a bit more of a dig around and found these;
                NotesDbDirectory dir = sess.GetDbDirectory("");
                NotesDatabase db = dir.OpenMailDatabase();
...did work OK, so that is good.
- Should there be some "tidy up" code to close the session when the application ends?

RE: "sess.GetDatabase always returned NULL no matter what I put into the Server and FileName"

Probably due to the Citrix environment.  I've never seen this fail before.

RE: "Should there be some "tidy up" code to close the session when the application ends?"

I'm not sure if it makes a difference in C#, but I usually set my Domino objects to null when I'm done with them.
stanfordaAuthor Commented:
I "normally" run Notes in Citrix, but to test this I ran locally... hence the need to configure the client on my local PC.

I'll just set my objects to null to finish. A lot of these type of objects (i.e. that consume network resources)  usually have at least a "dispose" method which would clean up any resources it was consuming. I guess the .Net Garbage Collector will tidy this up eventually if need be.

Very pleased to find a solution. Thanks for you help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now