Sample LS Class for sending EE mail

  1. Public Class EEMail
  2.    
  3.     Private JSONcontext List As String 'The JSON Paramenterd
  4.     Private m_HTMLBody As String 'The HTML Part of the message
  5.     Private m_gadgetURL As String 'The URL where the gadget.xml is at home (that needs to be authorized)
  6.     Private m_Subject As String ' The message subject
  7.     Private m_from As String 'The sender
  8.     Private m_session As NotesSession
  9.     Private m_dynamicHTML As Boolean 'Generate from Parameters
  10.     Private m_description As String ' The EE description
  11.    
  12.     %REM
  13.         Sub new
  14.         Description: Create a new EEMail
  15.     %END REM
  16.     Public Sub New()
  17.         Set m_session = New NotesSession
  18.         m_from = m_session.Username
  19.         m_dynamicHTML = true
  20.     End Sub
  21.    
  22.     %REM
  23.         Property Set isDynamic
  24.         Description: true = content generated from parameters
  25.     %END REM
  26.     Public Property Set isDynamic As Boolean
  27.         m_dynamicHTML = isDynamic
  28.     End Property
  29.    
  30.     %REM
  31.         Property Set description
  32.     %END REM
  33.     Public Property Set description As String
  34.         m_description = description
  35.     End Property
  36.    
  37.     %REM
  38.         Property Set Body
  39.         Description: THe HTML Message
  40.     %END REM
  41.     Public Property Set Body As String
  42.         m_HTMLBody = Body
  43.     End Property
  44.    
  45.     %REM
  46.         Property Set gadgetURL
  47.         Description: where the gadget URL lives
  48.     %END REM
  49.     Public Property Set gadgetURL As String
  50.         m_gadgetURL = gadgetURL
  51.     End Property
  52.    
  53.     %REM
  54.         Property Set subject
  55.         Description: Comments for Property Set
  56.     %END REM
  57.     Public Property Set subject As String
  58.         m_Subject = subject
  59.     End Property
  60.    
  61.     %REM
  62.         Sub addParameter
  63.         Description: add a JSON Parameter to the context
  64.     %END REM
  65.     Public Sub addParameter(pName As String, pValue As Variant)
  66.         Dim workString
  67.         Dim first As Boolean
  68.         first = true
  69.         If IsArray(pValue)Then
  70.             workString = |{|
  71.             ForAll ele In pValue
  72.                 If Not first Then
  73.                     workString = workString + |, |
  74.                 End If
  75.                 first = false
  76.                 workString = workString + |"|
  77.                 workString = workString + CStr(ele)
  78.                 workString = workString + |"|
  79.             End ForAll
  80.             workString = workString + |}|
  81.             JSONcontext(pName) = |"|+pName+|" : "| + workString +|"|
  82.         Else
  83.             JSONcontext(pName) = |"|+pName+|" : "|+CStr(pValue)+|"|
  84.         End if     
  85.     End Sub
  86.    
  87.     %REM
  88.         Sub sendMail
  89.         Description: Send the message
  90.     %END REM
  91.     Public Sub sendMail(SendTo As Variant)
  92.         Dim db As NotesDatabase
  93.         Dim mbox As NotesDatabase
  94.         Dim mail As NotesDocument
  95.         Dim mime As NotesMIMEEntity
  96.        
  97.         set db = m_session.Currentdatabase
  98.         Set mbox = New NotesDatabase(db.Server,"mail.box") 'Breaks on multi-mailbox servers
  99.        
  100.         If Not mbox.Isopen Then
  101.             Call mbox.Open("", "")
  102.         End If
  103.        
  104.         m_session.Convertmime = False
  105.         Set mail = mbox.Createdocument()   
  106.  
  107.         'Header values
  108.         Set mime = mail.CreateMIMEEntity("Body")
  109.         Call me.addHeader(mime, "Content-Type", "multipart/alternative")
  110.         Call me.addHeader(mime, "MIME-Version", "1.0")
  111.         Call me.addHeader(mime, "Content-transfer-encoding", "7bit")
  112.         Call me.addHeader(mime, "Subject", m_subject)      
  113.         Call me.addHeader(mime, "From", m_from)
  114.         ForAll rcp In SendTo
  115.             Call me.addHeader(mime, "To", CStr(rcp))       
  116.         End ForAll
  117.  
  118.         'Now text and embedded experience body
  119.         If Trim(me.m_HTMLBody) = "" Then
  120.             me.m_dynamicHTML = True 'Without content we generate dynamically
  121.         End If
  122.        
  123.         If me.m_dynamicHTML Then
  124.             Call me.setDynamicHTMLBody(mime)
  125.         Else
  126.             Call me.setHTMLBody(mime)  
  127.         End If
  128.        
  129.         Call me.setEEBody(mime)
  130.        
  131.         Call mail.Closemimeentities(true)
  132.        
  133.         'Normal Notes fields
  134.         Call mail.ReplaceItemValue("Form","Memo")      
  135.         Call mail.replaceItemValue("Recipients", SendTo)
  136.         Call mail.replaceItemValue( "From", m_from )
  137.        
  138.         Call mail.Save(true, False) 'We deposit directly into the mail.box
  139.  
  140.         m_session.Convertmime = True
  141.                        
  142.     End Sub
  143.    
  144.     Public Property Set Sender As String
  145.         m_from = sender
  146.     End Property
  147.    
  148.     %REM
  149.         Function setHTMLBody
  150.         Description: Get the human readable body of the eMail
  151.     %END REM
  152.     Private Sub setHTMLBody(mime As NotesMIMEEntity)
  153.         Dim out As NotesStream
  154.         Dim htmlbody As NotesMIMEEntity
  155.         Set out = m_session.Createstream()
  156.         out.WriteText(m_HTMLBody)
  157.         out.position = 0
  158.         Set htmlbody = mime.CreateChildEntity
  159.         Call htmlbody.SetContentFromText(out,"text/html; charset=UTF-8", ENC_IDENTITY_7BIT)            
  160.     End Sub
  161.    
  162.     %REM
  163.         Function setDynamicHTMLBody
  164.         Description: Create a human readable body of the eMail
  165.     %END REM
  166.     Private Sub setDynamicHTMLBody(mime As NotesMIMEEntity)
  167.         Dim out As NotesStream
  168.         Dim htmlbody As NotesMIMEEntity
  169.         Dim seperatorLcation As integer
  170.         Set out = m_session.Createstream()
  171.         Call out.WriteText(|<html>|,EOL_PLATFORM)
  172.         Call out.WriteText(|<head><title>|,EOL_PLATFORM)
  173.         Call out.WriteText(me.m_Subject,EOL_PLATFORM)
  174.         Call out.WriteText(|</title><style type="text/css">|,EOL_PLATFORM)
  175.         Call out.WriteText(|html, body, th {font-family : Verdana, Arial, sans-serif; font-size : small;}|,EOL_PLATFORM)
  176.         Call out.WriteText(|table {border : none; width : 60%}|,EOL_PLATFORM)      
  177.         Call out.WriteText(|</style></head><body>|,EOL_PLATFORM)
  178.         Call out.WriteText(|<h3>|,EOL_PLATFORM)
  179.         Call out.WriteText(me.m_Subject,EOL_PLATFORM)
  180.         Call out.WriteText(|</h3><p>|,EOL_PLATFORM)
  181.         Call out.WriteText(me.m_description,EOL_PLATFORM)
  182.         Call out.WriteText(|</p><table width="60%" border="0" style="background-color : #FFFEFF"><tr><th>Parameter</th><th>Value</th></tr>|,EOL_PLATFORM)
  183.         ForAll ctp In JSONcontext
  184.             seperatorLcation = InStr(ctp,":")
  185.             Call out.WriteText(|<tr><td>|,EOL_NONE)
  186.             Call out.WriteText(Left$(ctp,seperatorLcation-1) ,EOL_NONE)
  187.             Call out.WriteText(|</td><td>|,EOL_NONE)
  188.             Call out.WriteText(Mid$(ctp,seperatorLcation+1),EOL_NONE)
  189.             Call out.WriteText(|</td></tr>|,EOL_PLATFORM)
  190.         End ForAll
  191.         Call out.WriteText(|</table></body></html>|,EOL_PLATFORM)
  192.         out.position = 0
  193.         Set htmlbody = mime.CreateChildEntity
  194.         Call htmlbody.SetContentFromText(out,"text/html; charset=UTF-8", Enc_identity_7bit)
  195.         Call out.Close()   
  196.     End Sub
  197.    
  198.     %REM
  199.         Function setHTMLBody
  200.         Description: Get the human readable body of the eMail
  201.     %END REM
  202.     Private Sub setEEBody(mime As NotesMIMEEntity)
  203.         Dim out As NotesStream
  204.         Dim eebody As NotesMIMEEntity
  205.         Dim first As Boolean
  206.         Set out = m_session.Createstream()
  207.         'Here the JSON gets written out
  208.                
  209.         Call out.WriteText(|"{ gadget" : "|,EOL_NONE)
  210.         Call out.WriteText(m_gadgetURL,EOL_NONE)
  211.         Call out.WriteText(|",|,EOL_PLATFORM)
  212.        
  213.         Call out.WriteText(|"context" : {|,EOL_PLATFORM)
  214.         'Looping through context parameter
  215.         first = true
  216.         ForAll ctp In JSONcontext
  217.             If Not first Then
  218.                 Call out.WriteText(|, |,EOL_NONE)
  219.             End If
  220.             first = false
  221.             Call out.WriteText(ctp,EOL_PLATFORM)
  222.         End ForAll     
  223.         Call out.WriteText(|} } |,EOL_PLATFORM)
  224.        
  225.         out.position = 0
  226.         Set eebody = mime.CreateChildEntity
  227.         Call eebody.SetContentFromText(out,"application/embed+json; charset=UTF-8", Enc_identity_7bit)
  228.         Call out.Close()   
  229.     End Sub
  230.    
  231.     %REM
  232.         Sub addHeader
  233.         Description: Adds a header to a MIME Part
  234.     %END REM
  235.     Private Sub addHeader(mime As NotesMIMEEntity, hName As String, value As String)
  236.         Dim header As NotesMIMEHeader
  237.         Set header = mime.CreateHeader(hName)
  238.         Call header.SetHeaderVal(value)
  239.     End Sub
  240.    
  241.     %REM
  242.         Sub addHeader
  243.         Description: Adds a header to a MIME Part
  244.     %END REM
  245.     Private Sub addHeaderParam(mime As NotesMIMEEntity, hName As String, value As String)
  246.         Dim header As NotesMIMEHeader
  247.         Set header = mime.CreateHeader(hName)
  248.         Call header.Setheadervalandparams(value)
  249.     End Sub
  250.    
  251. End Class