Lotusscript: Mail Notification Class
Back in August 2009, I posted a small class I had created to make it easier for me to create mail notifications in my Lotusscript agents. Since then I have improved on it, and I wanted to post the latest version. One recent feature I added is to send external mail to the internet that looks like it is coming from a different user. This is often needed when you have an agent signed with a developer/admin ID, but you want the mail to look like it is coming from a particular department or user.
This is done by using the (unsupported) method of saving the document in mail.box instead of using doc.Send() to mail it directly. What I found is that you should use mail.box on the same server as the agent is running on, you can not access mail.box on a different server. The code reflect this.
Other new features are support for doclinks, attachments and an option to display a warning message at the end that the mail comes from an un-monitored role account. This warning is actually turn on by default.
Here is an example of how to use the class:
Option Public Option Declare Use "Class.MailNotification" Sub Initialize Dim mail As NotesMail ' *** Create a mail Set mail = New NotesMail() ' Set receipient and subject mail.MailTo = "texasswede@example.com" Call mail.AddMailTo("texasswede2@example.com") mail.Subject = "Please Read This" mail.Principal = "bogus_user@example.com" ' Create body Call mail.AppendText("Testing email...") Call mail.AppendNewLine(2) Call mail.Send() End Sub
And here is the class itself. Enjoy!
Option Public Option Declare Class NotesMail Public maildoc As NotesDocument Public body As NotesRichTextItem Private p_subject As String Private p_sendto List As String Private p_copyto List As String Private p_blindcopyto List As String Private p_principal As String Public NoReply As Integer Public mailbox As NotesDatabase Public Sub New() Dim session As New NotesSession Dim mailservername As String mailservername = session.Currentdatabase.Server ' Using mail.box directly is unsupported, but is the ' only way to make the mail look like it is actually ' sent from another address, in our case the principal. Set mailbox = New NotesDatabase(mailservername,"mail.box") If mailbox.Isopen = False Then Print "mail.box on " & mailservername & " could not be opened" Exit Sub End If Set me.maildoc = New NotesDocument(mailbox) Call me.maildoc.ReplaceItemValue("Form","Memo") Set me.body = New NotesRichTextItem(maildoc,"Body") me.p_subject = "" me.p_principal = "" ' Empty lists for addresses Erase me.p_sendto Erase me.p_copyto Erase me.p_blindcopyto me.NoReply = False ' Default is to add a disclaimer to the end End Sub Public Property Set Subject As String me.p_subject = FullTrim(Subject) End Property Public Property Get Subject As String Subject = me.p_subject End Property Public Property Set Principal As String me.p_principal = FullTrim(Principal) End Property Public Property Get Principal As String Principal = me.p_principal End Property '*** Recipient address (mailto) functions *** Public Property Set MailTo As String me.p_sendto(FullTrim(MailTo)) = FullTrim(MailTo) End Property Public Property Get MailTo As String ' Get the first address only ForAll mto In me.p_sendto MailTo = mto Exit ForAll End ForAll End Property Public Property Set SendTo As String ' Alias for MailTo MailTo = SendTo End Property Public Property Get SendTo As String ' Alias for MailTo SendTo = MailTo End Property Public Sub AddMailTo(address As String) ' Additional address me.p_sendto(address) = address End Sub Public Sub RemoveMailTo(address As String) ' Remove address Erase me.p_sendto(address) End Sub ' *** Functions for CC address *** Public Property Set MailCC As String me.p_copyto(FullTrim(MailCC)) = FullTrim(MailCC) End Property Public Property Get MailCC As String ' Get the first address only ForAll mcc In me.p_copyto MailCC = mcc Exit ForAll End ForAll End Property Public Sub AddMailCC(address As String) me.p_copyto(address) = address End Sub Public Sub RemoveMailCC(address As String) Erase me.p_copyto(address) End Sub ' *** Functions for BCC address *** Public Sub AddMailBCC(address As String) me.p_blindcopyto(address) = address End Sub Public Property Set MailBCC As String me.p_blindcopyto(FullTrim(MailBCC)) = FullTrim(MailBCC) End Property Public Property Get MailBCC As String ' Get the first address only ForAll bcc In me.p_blindcopyto MailBCC = bcc Exit ForAll End ForAll End Property Public Sub RemoveMailBCC(address As String) Erase me.p_blindcopyto(address) End Sub ' *** Functions for email body *** Public Sub AppendText(bodytext As String) Call me.body.AppendText(bodytext) End Sub Public Sub AppendDocLink(doc As NotesDocument, comment As String, linktext As String) If FullTrim(linktext) = "" Then Call me.body.AppendDocLink(doc, comment) Else Call me.body.AppendDocLink(doc, comment, linktext) End If End Sub Public Sub AppendNewLine(cnt As Integer) Call me.body.AddNewline(cnt) End Sub Public Sub AddNewLine(cnt As Integer) Call me.body.AddNewline(cnt) End Sub Public Sub AttachFile(filename As String) Call me.body.EmbedObject(1454,"",filename) End Sub ' *** Send the mail Public Sub Send() Dim session As New NotesSession Dim richStyle As NotesRichTextStyle If me.subject<>"" Then maildoc.Subject = me.subject End If If ListItemCount(me.p_sendto)>0 Then maildoc.SendTo = ListToArray(me.p_sendto) maildoc.Recipients = ListToArray(me.p_sendto) End If If ListItemCount(me.p_copyto)>0 Then maildoc.CopyTo = ListToArray(me.p_copyto) End If If ListItemCount(me.p_blindcopyto)>0 Then maildoc.BlindCopyTo = ListToArray(me.p_blindcopyto) End If If me.p_principal<>"" Then Call maildoc.ReplaceItemValue("Principal", me.p_principal) ' If principal is set, we want to fix so mail looks like ' it is coming from that address, need to set these fields Call maildoc.ReplaceItemValue("From", me.p_principal) Call maildoc.ReplaceItemValue("Sender", me.p_principal) Call maildoc.ReplaceItemValue("ReplyTo", me.p_principal) Call maildoc.ReplaceItemValue("SMTPOriginator", me.p_principal) End If ' If NoReply is set (default), append red warning... If NoReply = True Then Set richStyle = session.CreateRichTextStyle richStyle.NotesFont = 4 richStyle.NotesColor = 2 richStyle.Bold = True Call me.body.AppendStyle(richStyle) Call me.body.AddNewLine(1) Call me.body.AppendText("*** DO NOT REPLY TO THE SENDER OF THIS MESSAGE! ***") Call me.body.AddNewLine(1) Call me.body.AppendText("*** IT IS AN AUTOMATED SYSTEM MAIL ***") End If Call maildoc.ReplaceItemValue("PostedDate",Now()) If me.p_principal<>"" Then Call maildoc.Save(True,False) ' Save in mail.box Else Call maildoc.Send(True) ' Send mail normally End If End Sub ' *** Private functions called from within the class *** ' *** Convert list to array Private Function ListToArray(textlist As Variant) As Variant Dim i As Integer Dim temparray() As String ReDim temparray(0) As String ForAll t In textlist temparray(UBound(temparray)) = t ReDim Preserve temparray(UBound(temparray)+1) As String End ForAll ListToArray = FullTrim(temparray) End Function ' *** Count items in a list Private Function ListItemCount(textlist As Variant) As Integer Dim cnt As Integer cnt = 0 ForAll t In textlist cnt = cnt + 1 End ForAll ListItemCount = cnt End Function End Class
Update: Please don’t use the email addresses in the example code above to actually send email…