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

Posted on 2009-04-26
Last Modified: 2013-12-18
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

Question by:Hoboly
    LVL 63

    Expert Comment

    What version of the client is being used ?

    Maybe a newer version will work better ?


    Author Comment

    lotus notes 6.5.4

    Author Comment

    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.
    LVL 46

    Expert Comment

    by:Sjef Bosman
    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 .
    LVL 22

    Accepted Solution

    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?

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This is an old article, please see an updated version of this article, located here:
    I thought it will be a good idea to make a post as it will help in case someone else faces these issues. I trust this gives an idea how each entry in Notes.ini can mean a lot for the Domino Server to be functioning properly. This article discusses t…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now