Solved

Querying received e-mails from an agent

Posted on 2014-12-04
7
204 Views
Last Modified: 2014-12-04
Hi,

I'm new to Lotus Notes development and I'm trying to write an agent that when triggered searches a mail database of any received e-mails with a specified time range.

I figured out how to query by date range but I don't know how to limit the search to received e-mails only (e.g. no drafts, not sent items, etc.).

Any help would be greatly appreciated.

Thank you in advance.

Alex
0
Comment
Question by:fhios
  • 4
  • 3
7 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40480837
How do you do the search, which method do you use: NotesDatabase.Search, NotesDatabase.FTSearch, NotesView.FTSearch, or do you use a view that your code traverses from beginning to end, maybe with a GetElementsByKey with a date?

In all cases: check the Sent view in the mail database, more precisely the Select statement. That's what you need to copy to your Search. If you use FTSearch, the syntax is different but the same can be accomplished (usually).

Could you show us some leg, er... some code of the agent? Thanks.
0
 

Author Comment

by:fhios
ID: 40480942
Here is a snippet of the code in the agent.

The logic within the callback marks the document as processed by calling
context.updateProcessedDoc(document);

Open in new window

.

	public <T> int withEachDocumentFromBatch(AgentContext context, AgentLog log, DocumentCallback<T> callback) throws Exception {
		int processedCount = 0;
		DocumentCollection unprocessedDocuments = context.unprocessedSearch([NEED TO FIGURE OUT THE QUERY HERE]);
		log.info("Documents found in 'batch' mode");

		int count = (unprocessedDocuments != null) ? unprocessedDocuments.getCount() : 0;
		log.info("Found " + count + " documents");

		for (int i = 1; i <= count; i++) {
			try {
				Document document = unprocessedDocuments.getNthDocument(i);
				try {
					callback.call(context, document);
				} finally {
					recycle(log, document);
				}
				// printDebugInfo(document);
				processedCount++;
			} catch (Throwable e) {
				log.error("Unable to process document number [" + i + "]", e);
			}
		}
		return processedCount;
	}

Open in new window


Thanks
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40481298
I have some difficulty in understanding your code (Java isn't one of my stronger points, I'm afraid, but I'm working on it). Assuming you are developing a Java Agent using the Domino Designer, there are some elements I don't know: AgentLog log (your own instead of the Log class?), the unprocessedSearch method (your invention?), recycle() which probably should be document.recycle(), but that's not the real issue here.

Here's a simple agent in Java that searches a database using a linear search method:
http://www-01.ibm.com/support/knowledgecenter/SSVRGU_9.0.1/com.ibm.designer.domino.main.doc/H_EXAMPLES_SEARCH_METHOD_JAVA.html

To access every document in a collection, you'd better use the getFirstDocument and getNextDocument methods. getNthDocument is slower, usually, but it should work all-right. An example as well:
http://www-01.ibm.com/support/knowledgecenter/SSVRGU_9.0.1/com.ibm.designer.domino.main.doc/H_EXAMPLES_GETNEXTDOCUMENT_METHOD_COLLECTION_JAVA.html

The Select statement in the Sent view is
SELECT DeliveredDate = "" & PostedDate != "" & !(@IsMember("S"; ExcludeFromView))
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:fhios
ID: 40481775
Hi,

AgentLog is my own class that uses org.apache.log4j.net.SocketAppender to send the logs to a centralised server.

recycle() is a defensive version of document.recycle() that handles exceptions more gracefully.

The getFirstDocument and getNextDocument methods, unfortunately they don't seem to be working from JAVA.

unprocessedSearch is a method in the Lotus Notes JAVA API that takes as a first parameter a "A Lotus Domino formula that defines the selection criteria."

Looking at the formula from the sent view, I would need to invert the criteria because I need to search for incoming e-mails rather than outgoing. Does the following make sense?

SELECT DeliveredDate != "" & PostedDate = "" & (@IsMember("S"; ExcludeFromView))

Open in new window


What does (@IsMember("S"; ExcludeFromView)) do?

Thanks
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 500 total points
ID: 40481824
Now why on earth did I take the Sent view... Goodness, what was I thinking? Never mind, humble apologies.

There are 2 ways to get received mails, but the approach is different, and so might be the number of mails found. If you want really all mails received, there is this query (source: Richard Schwarz):

SELECT ( @IsNotMember("A"; ExcludeFromView) & IsMailStationery != 1 & Form != "Group" & Form != "Person" ) & !( DeliveredDate = "" & PostedDate != "" & !(@IsMember("S"; ExcludeFromView)) )

If you can do with the messages that are in the Inbox at the moment of the search (the Inbox is a folder, mails can be moved out of this folder to other folders), you don't need to do a search: you can just open the Inbox view and walk through it (using getFirst and getNext calls).

The IsMember "S" tests if the mail is in the Sent folder.

Strange that getFirstDocument and getNextDocument don't work, it's the first time I hear of this.

And a warning. Do not rely too much on "unprocessed documents": once processed they can revert to unprocessed easily, e.g. when you change the agent code and save it. Or when someone switches the agent off and back on (when scheduled). It might be better to use a profile document somewhere, where you save the current cutoff date and time. You need another agent (can be in Formula language) if you want to change that cutoff date manually.
0
 

Author Comment

by:fhios
ID: 40481999
Thank you, I'll play around following your suggestions.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40482069
I'd love to hear from you when you cracked it. Thanks!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question