Lotus Notes Send EMail from VB or VBA
Original doc link:http://www.fabalou.com/VBandVBA/lotusnotesmail.asp
你知道吗?VBA或者VB可以直接编程从发送邮件到Notes系统中,当然前提条件是在运行代码的这台机器上必须安装Notes 客户端并且完成配置.
要指出的一个问题那就是所有 Lotus Notes 对象声明都必须是后期绑定(late bound或late-binding),不然就会报错,(不知道为什么,也许是Lotus Notes API的BUG吧). 你可以随意使用这段代码,但是请注意保留上面的连接在你的网页上。很多人无礼的去掉版权说明而一字不改的照搬代码甚至还宣称是原创的,我觉得这种行为是不齿的,如果被我发现有人这样做那就有好戏看了。。。(* 原文如此 据我理解,后期绑定指的是:不预先定义好长整、布尔等数据类型,而是在执行过程中根据需要来动态分配内存,这样可能会降低效率,但是对程序员可就能省下些时间了)
还有一点值得朋友们注意. 在 5.x 版本上运行到 MailDoc.CREATERICHTEXTITEM ("Attachment") 时, 可能会出现问题,把这行去掉就可以了. 前面那行代码就足以完成此操作了. 为什么4.x 上只能用这个方法,我也找不到原因,似乎又要归结为BUG了。
'Public Sub SendNotesMail(Subject as string, attachment as string, 'recipient as string, bodytext as string,saveit as Boolean) '函数功能:发送带附件的邮件给recipient变量中指定的收件人 '运行环境:安装并配置好Notes客户端. Public Sub SendNotesMail(Subject As String, Attachment As String, Recipient As String, BodyText As String, SaveIt As Boolean) '设置对象属性Set up the objects required for Automation into lotus notes Dim Maildb As Object 'The mail database Dim UserName As String 'The current users notes name Dim MailDbName As String 'THe current users notes mail database name Dim MailDoc As Object 'The mail document itself Dim AttachME As Object 'The attachment richtextfile object Dim Session As Object 'The notes session Dim EmbedObj As Object 'The embedded object (Attachment) '创建Notes会话 Set Session = CreateObject("Notes.NotesSession") '就想帮助文件里面提到的那样,COM用户必须先初始化会话方可继续Domino对象的操控,仅适用于 5.x 以上版本. Session.Initialize("password") '取得用户名并计算邮件文件名 '在某些情况,假如你传递一个空字符串到 MailDBname 变量,一样能够发送邮件,只要ID口令正确就可以了. UserName = Session.UserName MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf" '打开Notes邮箱 Set Maildb = Session.GETDATABASE("", MailDbName) If Maildb.ISOPEN = True Then '判断已经打开 Else Maildb.OPENMAIL End If '创建新邮件 Set MailDoc = Maildb.CREATEDOCUMENT MailDoc.Form = "Memo" MailDoc.sendto = Recipient MailDoc.Subject = Subject MailDoc.Body = BodyText MailDoc.SAVEMESSAGEONSEND = SaveIt '设置嵌入对象,添加附件 If Attachment <> "" Then Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment") Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment") 'Attachment格式为:c:\my documents\report.doc '下一行要注释掉,不然会出现"Rich text item Attachment already exists."的错误提示 MailDoc.CREATERICHTEXTITEM ("Attachment") End If '发送文档 MailDoc.PostedDate=Now() '加上PostedDate,邮件就会出现在发件箱 MailDoc.SEND 0, Recipient MsgBox "发送完毕!" '清理状态 Set Maildb = Nothing Set MailDoc = Nothing Set AttachME = Nothing Set Session = Nothing Set EmbedObj = Nothing End Sub |
|
如果想把邮件发给几个人,用抄送或者密送就可以了:
MailDoc.sendto = Recipient MailDoc.CopyTo = ccRecipient MailDoc.BlindCopyTo = bccRecipient
|
如果我们用数组变量,实际应用处理更多的收件人可能会更加灵活(* 注意sendto域不要超过64K的上限)
Dim recip(25) as variant recip(0) = "emailaddress1" recip(1) = "emailaddress2" '等等.. maildoc.sendto = recip |
不再局限于LotusScript的视野,对Domino COM编程感兴趣的朋友也许可以参考另外两篇文章:
《VB操作Lotus Notes数据库》http://www.enet.com.cn/eschool/inforcenter/3680_A20040305291853.html
Common Ground:COM access to Domino objects http://www.lotus.com/ldd/today.nsf/Lookup/COM_Access
How to send mail from VB to multiple users http://forums.devshed.com/archive/t-89323
"Component Object Model (COM) Features Added in Release 5.0.2b" (#177288).
http://www.groupcomputing.com/dominoproforum.nsf/0/3d8d0a73dab9b88c85256e68007c1e65?OpenDocument