Tuesday, May 11, 2010

Sending Mails With Rich Text Tables from XPages

I am amazed at the extent to which the lotus script libraries are mimicked in the Server Side Javascript libraries in XPages . The following is a simple illustration of a scenario that amazed me --- sending mails with rich text tables from an XPage.

Copy and paste the following code fragment into a XPage button control, preview the same on the Browser or Notes Client and you are ready to go.
Ensure that you give your own mail address so that you can check the functionality

/*******************************************************/

//change the mail address to test it for your self
var mailAddress="ozinisle.inferno@blogspot.com";

var currDB: NotesDatabase=session.getCurrentDatabase();
var mailDoc:NotesDocument=currDB.createDocument();
var bodyRt:NotesRichTextItem=mailDoc.createRichTextItem("Body");
var RTELEM_TYPE_TABLECELL =7;

mailDoc.replaceItemValue("Form","Memo");
mailDoc.replaceItemValue("SendTo",mailAddress);
mailDoc.replaceItemValue("Subject","XPage Mail");

bodyRt.appendTable(3,4);
bodyRt.update();

var bodyRtNav:NotesRichTextNavigator=bodyRt.createNavigator();
if (bodyRtNav.findFirstElement(RTELEM_TYPE_TABLECELL))    {
    bodyRt.beginInsert(bodyRtNav);
    bodyRt.appendText("hi this is my value");
    bodyRt.endInsert();
}    else {
    getComponent("errs").setValue('no table found');
}

try {
    if (bodyRtNav.findNextElement(RTELEM_TYPE_TABLECELL))    {
        bodyRt.beginInsert(bodyRtNav);
        bodyRt.appendText("hi this is my other value");
        bodyRt.endInsert();   
    }
}    catch (exp)    {
    getComponent("errs").setValue(getComponent("errs").getValue()+ @NewLine()+'no table found 2');
}

bodyRt.update();

mailDoc.send();



/*******************************************************/

Hope this helps :)

15 comments:

  1. How to attach more than one files into mail document...

    ReplyDelete
  2. @Syed: take a look into the following post - http://ozinisle.blogspot.com/2010/06/add-multiple-attachments-to-rich-text.html

    ReplyDelete
  3. When i use these codes(posted by you for sending mails), i am getting below error:

    Exception

    Error while executing JavaScript action expression

    Script interpreter error, line=36, col=9: Exception occurred calling method NotesDocument.send() null

    ReplyDelete
  4. @Nice : The code seems to work fine for me....

    I did a little test and it is send out mails with out any issues....

    Can you plz elaborate on how you worked with the code

    ReplyDelete
  5. How do I embed fields from my xpage into the email? I have fields like employee name and department in on my page.

    ReplyDelete
  6. When i use these codes(posted by you for sending mails), i am getting below error:

    Exception

    Error while executing JavaScript action expression

    Script interpreter error, line=36, col=9: Exception occurred calling method NotesDocument.send() null

    ReplyDelete
  7. Hi Aboobacker,

    Guess you have reused the code as such. 'Nice' had reported the same issue and I guess he did the same.

    I have mentioned the sendTo address as "mailAddress". Try changing that to your email address

    Hope that helps

    ReplyDelete
  8. Dear Sir ,

    I have same problem like Nice & Aboobacker (Script interpreter error, line=36, col=9: Exception occurred calling method NotesDocument.send() null)

    How to fix this problem ?

    ReplyDelete
  9. post your code. Let me analyse it before answering

    ReplyDelete
  10. Dear Karthikeyan :

    I solved my Problem.
    Your sample Code is perfect, problem is Server name,my Server name include underscore char , casuse this problem.very thanks for your help.
    Jeffrey wu.

    ReplyDelete
  11. nice to know. Thanks for sharing the same

    ReplyDelete
  12. Hi Karthikeyan, the error occured because, Mail sending from web browser was restricted at server level.

    ReplyDelete
  13. Interesting. Never tried such server level restriction before. Will look into it and modify the sample with error handlers if time permits :) Thanks a lot for sharing the same

    ReplyDelete