running out of resource after calling lotus notes api many times~~

I wrote a robot program to call lotus notes API to send emails by different email a/c
the program works very well for the first few hours
but after around 4 hours, it is unable to call lotus notes API to call email (error in the following open function)

I suspect that I need to close / dispose the lotus notes API object.
but I have no ideas what need to close / dispose.

Could anyone help me, please?
Public Sub sendEmail()
        Dim recep As String() = ssMailTo
        Dim ccRecipient As String() = New String() {}
        Dim subj As String = sSubjectSend
        Dim mailbody As String = "This is robot message." '"hi" + ControlChars.NewLine + "bye"
        Dim savemsg As Boolean
        Dim domNotesDocumentMemo As NotesDocument
        Dim DomNotesItem As NotesItem
        Dim sUser As String
        Dim objNotesRichTextItem As NotesRichTextItem
        ' Create a new memo document.
        domNotesDocumentMemo = domDB.CreateDocument
        Call domNotesDocumentMemo.ReplaceItemValue("Form", "Memo")
        Dim body As NotesMIMEEntity
        'body = domNotesDocumentMemo.CreateMIMEEntity
        'body.CreateHeader("To").SetHeaderVal(String.Join(";", recep))
        'body.SetContentFromText _
        ' (Nothing, "text/plain;charset=UTF-8", MIME_ENCODING.ENC_NONE)
        body = domNotesDocumentMemo.CreateMIMEEntity
        Dim header As Domino.NotesMIMEHeader
        header = body.CreateHeader("Subject")
        Call header.SetHeaderVal(subj)
        header = body.CreateHeader("To")
        Call header.SetHeaderVal(String.Join(";", recep))
        header = body.CreateHeader("From")
        Call header.SetHeaderVal(domS.UserName)
        header = body.CreateHeader("Replyto")
        Call header.SetHeaderVal(domS.UserName)
        Call domNotesDocumentMemo.ReplaceItemValue("$INETReplyTo", domS.UserName)
        Call domNotesDocumentMemo.ReplaceItemValue("ReplyTo", domS.UserName)
        Call domNotesDocumentMemo.ReplaceItemValue("Principal", domS.UserName)
        Call domNotesDocumentMemo.ReplaceItemValue("$InetPrinicipal", domS.UserName)
        Call domNotesDocumentMemo.ReplaceItemValue("$AssistMail", Nothing)
        Call domNotesDocumentMemo.RemoveItem("$AssistMail")
        Call domNotesDocumentMemo.ReplaceItemValue("$TITLE", Nothing)
        Call domNotesDocumentMemo.ReplaceItemValue("InetSendTo", recep)
        'Call domNotesDocumentMemo.ReplaceItemValue("Form", "Memo")
        Call domNotesDocumentMemo.ReplaceItemValue("useApplet", "True")
        Call domNotesDocumentMemo.ReplaceItemValue("DefaultMailSaveOptions", "1")
        Call domNotesDocumentMemo.ReplaceItemValue("$StorageTo", "1")
        Call domNotesDocumentMemo.ReplaceItemValue("$Mailer", "Lotus Notes Release 6.5.4 March 27, 2005")
        'Call domNotesDocumentMemo.AppendItemValue("SendTo", "")
        ''Now get a handle on the item
        'DomNotesItem = domNotesDocumentMemo.GetFirstItem("SendTo")
        ''Now pass your array
        'For Each r As String In recep
        '    If r <> "" Then
        '        Call DomNotesItem.AppendToTextList(r)
        '    End If
        Call domNotesDocumentMemo.ReplaceItemValue("SendTo", recep)
        Call domNotesDocumentMemo.ReplaceItemValue("CopyTo", ccRecipient)
        Call domNotesDocumentMemo.AppendItemValue("Subject", subj)
        'objNotesRichTextItem = domNotesDocumentMemo.CreateRichTextItem("Body")
        'Dim body As NotesMIMEEntity
        ''body = domNotesDocumentMemo.CreateMIMEEntity
        ''body.CreateHeader("To").SetHeaderVal(String.Join(";", recep))
        ''body.SetContentFromText _
        '' (Nothing, "text/plain;charset=UTF-8", MIME_ENCODING.ENC_NONE)
        'body = domNotesDocumentMemo.CreateMIMEEntity
        'Dim header As Domino.NotesMIMEHeader
        'header = body.CreateHeader("Subject")
        'Call header.SetHeaderVal("MIME message")
        'header = body.CreateHeader("To")
        'Call header.SetHeaderVal("Roberta Person")
        'Dim stream As Domino.NotesStream
        'Call stream.WriteText("Text of message.")
        'Call body.SetContentFromText _
        '(stream, "text/plain;charset=UTF-8", MIME_ENCODING.ENC_NONE)
        'Call objNotesRichTextItem.AppendText(mailbody)
        If Trim(LCase(savemsg)) = "yes" Then
            domNotesDocumentMemo.SaveMessageOnSend = True
            domNotesDocumentMemo.SaveMessageOnSend = False
        End If
        domNotesDocumentMemo = Nothing
    End Sub
    Public Function open() As Boolean
        IO.File.Copy(IO.Path.Combine(IO.Directory.GetCurrentDirectory, sIDfilePath), IO.Path.Combine(notesDataPath, sIDfilePath), True)
        IO.File.Copy(IO.Path.Combine(IO.Directory.GetCurrentDirectory, "notes.ini"), IO.Path.Combine(notesPath, "notes.ini"), True)
        WritePrivateProfileString("Notes", "KeyFilename", IO.Path.Combine(notesDataPath, sIDfilePath), IO.Path.Combine(notesPath, "notes.ini"))
        WritePrivateProfileString("Notes", "MailFile", sPfile, IO.Path.Combine(notesPath, "notes.ini"))
        domS = CreateObject("lotus.NotesSession")
        Call domS.Initialize(sPpassword)
        'domDir = domS.GetDbDirectory(sPserver)
        'domDB = domDir.OpenMailDatabase()
        domDB = domS.GetDatabase(sPserver, sPfile)
        'domDir = domS.GetDbDirectory(sPserver).OpenMailDatabase
        Return True
    End Function
    Public Sub close()
        domDB = Nothing
        domS = Nothing
    End Sub

Open in new window

Who is Participating?
Bill-HansonConnect With a Mentor Commented:
I see several things working against you here.

1. You are mixing early-bound and late-bound objects in the same module.

For example, in your "open" function, you are instantiating the NotesSession object using CreateObject (late-bound), but later in the sendMemo function, you are declaring a NotesMIMEHeader using the "Lotus Domino Objects" library (early-bound).  Next, you instantiate the header (early-bound) from the body (late-bound).  This could be the root of your problems.

2. You are right about garbage collection.  The dot net garbage collectors will not be able to free the resources instantiated via COM (early or late bound).  This means that you need to free the resources yourself wherever possible (although with COM, you can never be sure when an object is truly freed).

One thing that I notice is that you never close the database.  You set it to Nothing in "close", but you should call domDB.Close before setting it to Nothing (the NotesDocument.Close method is no longer in the API documentation for some reason, but it does exist).  If I leave this call out in my own apps, I also run out of resources.

Also, make sure you set all Notes objects to Nothing before exiting the sendMail function (domNotesDocumentMemo, body, header). Sjef is right about error handling.  How are you ensuring that all garbage is being collected when an error occurs?  If you're not comfortable with classes, at least use "On Error GoTo" to ensure that your objects are all set to Nothing.

3. I'm not sure if this is a problem or not, but I've never used MIME headers to set attributes such as Subject, To, From, or ReplyTo.  These are normally set as item values.  You are also setting the correct item values later in the code.  Is there some reason that you are doing it both ways?
What version of the client is being used ?

Maybe a newer version will work better ?

HobolyAuthor Commented:
lotus notes 6.5.4
HobolyAuthor Commented:
Cannot create ActiveX component.
at Microsoft.VisualBasic.Interaction.CreateObject(String ProgId, String ServerName)

        domS = CreateObject("lotus.NotesSession")
        Call domS.Initialize(sPpassword)
        domDB = domS.GetDatabase(sPserver, sPfile)
 after few hours.
Sjef BosmanGroupware ConsultantCommented:
Two questions:
1) If Notes is open, does it still function? If so, there's little to clean up in Notes.
2) Are you sure your function close() is called after every open(), and that there is no error handling preventing the close() to happen?

Suggestion: create a class around these functions, and use the close-function inside the Delete .
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.

All Courses

From novice to tech pro — start learning today.