邮件是Lotus Notes体系的核心和基本功能,以至于Send()是NotesDocument的一个方法,任何一个文档都可以被发送出去,Notes里的一封邮件也只是一个有一些特殊字段的文档。在程序开发中,发邮件的功能也被广泛和高度使用。Notes中发邮件的功能可以被开发到很精致和复杂的程度,适应几乎所有的需求。笔者准备分几篇文章讨论这些功能。
首先是最简单的在LotusScript中发送邮件,包括传统的客户端和Html邮件。给出用户、主题和内容,发送邮件,这是最典型的需求,自然也应该被写成函数。
下面这个函数的参数分别为收件人、抄送给、盲送给、主题、正文和附上的链接文档(如果没有则传入Nothing)。各个收件人参数的数据类型与文本型字段所能接受的一样,即字符串或字符串数组。
Public Sub CC(sendto As Variant,CopyTo As Variant,BlindCopyTo As Variant,subject As String,body As String,linkdoc As Variant)
Dim s As New NotesSession
Dim db As NotesDatabase
Set db=s.CurrentDatabase
Dim mailDoc As New NotesDocument(db)
With mailDoc
Call .ReplaceItemValue("Form", "memo")
Call .ReplaceItemValue("SendTo",sendto)
Call .ReplaceItemValue("CopyTo", CopyTo)
Call .ReplaceItemValue("BlindCopyTo", BlindCopyTo)
Call .ReplaceItemValue("Subject",subject)
End With
Dim rtItem As New NotesRichTextItem(mailDoc,"Body")
Call rtItem.AppendText(body)
If Not linkdoc Is Nothing Then
Call rtItem.AppendDocLink(linkdoc,"click the link")
End If
Call mailDoc.Send(False)
Dim msg As String
If IsArray(sendto) Then
msg=Join(sendto)
Else
msg=sendto
End If
Print "sent mail to " & msg
End Sub
因为LotusScript的函数不支持重载,下面这个函数接受更常见的收件人、主题、正文和附加的链接文档作为参数,调用起来省去两个参数更方便。
Public Sub SendMail(sendto As Variant,subject As String,body As String,linkdoc As Variant)
Call CC(sendto, "", "", subject, body, linkdoc)
End Sub
下面这个函数发送一封Html邮件,参数分别为收件人、抄送给、盲送给、主题和Html的正文。
Public Function CCHTMLMail(sendTo As Variant, copyTo As Variant, blindCopyTo As Variant, subject As String, html As String)
Dim s As New NotesSession
s.ConvertMIME=False
Dim db As NotesDatabase
Set db=s.Currentdatabase
Dim memo As NotesDocument
Set memo=db.Createdocument()
Call memo.appendItemValue("Form", "Memo")
Call memo.replaceItemValue("SendTo", sendTo)
Call memo.Replaceitemvalue("CopyTo", copyTo)
Call memo.Replaceitemvalue("BlindCopyTo", blindCopyTo)
Call memo.replaceItemValue("Subject", subject)
Dim stream As NotesStream
Set stream=s.CreateStream()
Call stream.writeText(html)
Dim entity As NotesMIMEEntity
Set entity=memo.createMIMEEntity("Body")
Call entity.setContentFromText(stream,"text/html;charset=UTF-8", 1725)
Call stream.close()
Call memo.Send(False)
s.ConvertMIME=True
End Function
最后这个函数同样省去了抄送给和盲送给两个参数,发送一封Html邮件。
Public Function SendHTMLMail(sendTo As Variant, subject As String, html As String)
Call CCHTMLMail(sendTo, "", "", subject, html)
End Function