Simple xPage Mail send using Lotus Script agent ( Method 2)

Simple XPage LotusScript mail send : You may have faced difficulty in mail send scripting from an XPage but there is one, best alternative for mail using simple lotus script mail send code.
First initialize the important classes like notes session, notes database and some string variables. Here the important is, while sending mail from an XPage using lotus script you have to take care for XPage web document. Earlier notes document context class (session.DocumentContext) to set web document.
But in XPage there is different method to get web document handle. The session.DocumentContext will not applicable to XPage web document. 
To get the handle of an XPage web document you have to first initialize the notes agent class and then use notes agent property “Parameterdocid” to get note id of an XPage document which we are using as webdocument.
This is about the server side scripting – now you have to call this agent from an XPage.
Open your XPage and find XPage event ‘postSaveDocument’
Remember you have to add JavaScript code on 'postSaveDocument' only, because it loads memory after XPage save event , with this its easily to get handle of XPage document.

Now select "Add Action.”button and select action list box and then find "Execute Script" and select it.
Select language as "javascript (serverside)" and add below code.
“Server Side JavaScript”
agent=database.getAgent("AgentQuerySave")  // it will set the current agent in server memory 
noteid=document1.getDocument().getNoteID() // get your current XPage note ID for document1 // run agent with selected note id 
“Server Side JavaScript”

Sub Initialize
    Dim session  As New NotesSession
    Dim  db As NotesDatabase
    Dim xpagedoc As NotesDocument
    Dim agent As NotesAgent
    Dim noteid As String
    Dim sendtolist As Variant
    Set db = session.CurrentDatabase   
    Set agent=session.Currentagent 
    Dim MailBodyText As String
    noteid=agent.Parameterdocid   'Important line-  it returns the document note 'Id for the document which we have passed in to the agent for execution i.e.  XPage
Set xpagedoc = db.GetDocumentByID(noteid) ' Important line, its takes  the  document note id to set current xpagedocument insted of documentcontext   
    Dim rtbodyitem As NotesRichTextItem '// XPage rich text item   
    Set rtbodyitem=xpagedoc.GetFirstItem("RichText")
    If Not rtbodyitem Is Nothing  Then
        Print “Check for mail send richtexi item"
        mailBodyText=rtbodyitem.GetFormattedText(True,0) ‘// get the formatted rich text         
    End If   
    '// your mail document setting starts
    Set maildoc = db.CreateDocument '// Create new maildoc for 'Memo' form 
    sendtolist=xpagedoc.SendTo '// Get the list of all recipient from XPage   
    maildoc.Form = "Memo" ' Set your mail Memo form
    maildoc.SendTo = sendtolist '// All send to names   
    maildoc.Subject = "Simple XPage Mail Send Using Lotus Script" '// your subject line   
    Set body = New NotesRichTextItem( maildoc, "Body") '// set mail body for your mail for (Memo form)
    Call body.AddNewLine( 1 )  
    If Trim(mailBodyText)<>"" Then Call body.AppendText(mailBodyText)   '// Append XPage mail item to your mail file mail body item   
    Call maildoc.Send(False) ' // Final mail send command   
End Sub

 'Use print command to print your messages to log.nsf  file and as your are families with lotus script code you can build /use / migrate you complex mail sending functionality to this simple mail send code and enjoy xpage development with lotus script.
Thanks for reading this article, hope you like it, if you have any difficulty please contact us on ‘

No comments :