This article tries to give a basic idea on how to send mails through Lotus Notes using Power Builder. This method uses the OLE object technique. Here, we use Lotus Notes' inbuilt classes to perform the required functions. We create objects of these classes as OLE objects. To know more about the Lotus Notes classes, please explore the following link.
- This code does not include proper error handing.
- This code was tested in Power Builder 10.0 (Build 4510) and Lotus Notes 6.5.
- Before running the code, please ensure that Lotus Notes is opened using the user machine procedure.
The following procedure uses the OLE object technique to send mails through Lotus Notes. The following OLE objects must be declared:
- Notes session object ("
- Notes database object ("
- Notes document object ("
- RTF object ("
notesRichTextItem") – this object is used to create attachments in Notes.
Step 1: Creating the connection to Lotus Notes
First, a connection to Lotus Notes has to be made using the "
notesSession = Create oleobject
Step 2: Connecting to the database
After a session has been created with Notes, a database has to be opened. This is done by the "
GetDatabase method creates a
NotesDatabase object that represents the database located at the server. The first parameter is the server name and the second parameter is the username. Usually, we have to specify the server and the user name. Giving these as blank will connect to the default server.
For the default server connection:
notesDatabase = notesSession.GetDatabase("","")
For other databases:
notesDatabase = notesSession.GetDatabase("<SERVER >"," <USERNAME >")
After connecting to the database, the mailbox has to be opened. This is done using the "
OpenMail" command. The
OpenMail method finds the current user's mail server and the database in the notes.ini file and opens it. It is a good practice to first check whether the current user’s database is open and then open it if it's not.
IF NOT notesDatabase.IsOpen THEN
Step 3: Creating the document
After the database has been opened, a new Notes document has to be created (in Notes, a new mail is represented as a document). This is done using the "
CreateDocument method creates a document in the database and returns a
NotesDocument object that represents the new document.
notesDocument = notesDatabase.CreateDocument
Setting the properties
Form to "
Memo" so that its recipient can read it as a mail memo:
notesDocument.Form = "Memo"
subject property to set the subject of the mail:
notesDocument.Subject = "Hello"
The body of the mail can be treated as a rich text item. Creating a new rich text item in a document is done using the "
CreateRichTextItem method creates a new rich text item in a document, using a name you specify, and returns the corresponding
notesRichTextItem = notesDocument.CreateRichTextItem("Body")
After creating the RTF object, the text can be appended to the body of the mail using the "
NotesRichTextItem.AppendText("Here comes the body of the mail")
Attachments can be added to the RTF object using the "
NotesRichTextItem.EmbedObject*(1454, "", "<Attachment Path>", "")
This command will add only one attachment to the mail body. To add more attachments, we need to reuse the same command. The third parameter would be blank and the first parameter would be set to 1454.
NotesRichTextItem.EmbedObject(1454, "", "C:\Attachment1.doc", "")
NotesRichTextItem.EmbedObject(1454, "", "C:\Attachment2.doc", "")
The recipient’s mail IDs are in the form of a string. In the case of multiple recipients, an array of strings should be used.
See the example below to set-up multiple recipients:
Recipient_arr = email@example.com
Recipient_arr = firstname.lastname@example.org
In order to set this list as the "To" list, use the "
NotesDocument. SendTo = Recipient_arr
In order to set this list as the "CC" list, use the "
NotesDocument.CopyTo = Recipient_arr
In order to set this list as the "BCC" list, use the "
Other properties of the document object
|Reserved Field Name
|L, N, H
||Values correspond to: low, normal or high-priority.|
|N, B, C, T
||Values correspond to: none, only on failure, confirm delivery, trace entire path|
||Use 1 to encrypt mailed documents.|
||Use 1 to send a receipt when the document is opened by the recipient.|
||Use 1 to add an electronic signature to the fields. (Only applicable if a form also contains sign-enabled fields.)|
|If true the document will be saved to the send item list.|
|Today’s date time
||In PB, this can be retrieved using the "|
Note: Here, even the numeric values should be set as characters, e.g.:
NotesDocument.ReturnReceipt = ‘1’
Saving the document
Save method is used to save any changes made to the document.
NotesDocument.Save(TRUE, FALSE ,TRUE)
notesDocument.Save( force, createResponse [, markRead ] )
True, the document is saved even if someone else edits and saves the document while the script is running. The last version of the document that was saved wins; the earlier version is discarded. If
False, and someone else edits the document while the script is running, the
createResponse argument determines what happens to the document.
True, the current document becomes a response to the original document (this is what the replicator does when there's a replication conflict). If
False, the save is canceled. If the
force parameter is
createResponse parameter has no effect.
True, the document is marked as read. If
False (default), the document is not marked as read.
Step 4: Sending the document
When the document is prepared, it is sent using the "
Now, the process of sending the mail is completed.
Step 5: Cleanup
After sending the mails, the user needs to free the memory and close all the connections. This involves disconnecting the session object and destroying the OLE objects: