您的位置:首页 > Web前端 > HTML

如何使用 LotusScript 代理来发送 HTML 格式的邮件

2014-11-28 17:34 681 查看

Technote (troubleshooting)

问题

如何使用 LotusScript 来发送 HTML 格式的邮件消息?
在 Notes 和 Domino Designer 6.x及以上版本中拓展了 NotesMIMEEntity 类,此前使用定时代理来发送 HTML 格式的邮件是不太可能的。当你把 HTML 代码加入到邮件主体中,即使设置了 NotesRichTextStyle 属性为 PassThruHTML,Notes仍然会把邮件作为原始的 HTML 代码发送。

解决问题

Domino 6 以上版本中 NotesMIMEEntity 类的方法和属性,以及新增的 NotesStream 类,使得通过定时代理发送 HTML 格式的邮件成为可能。这对于寄送 HTML 新闻邮件或者给邮件数据库提交信息的用户自动回复很有帮助。你可以创建一个代理程序,发送存储在本地文件系统上的 HTML 或动态创建 HTML。下面的代理举例说明了上述功能。

重要注释:以下代码只是一个样例,证实了实现这项功能的一种方法,如需使用则用户自己承担相应的风险。为了保证该样例能够正常工作,必须严格按照上面的写法去执行,产品支持部门不支持对该代码的定制。


使用本地 HTML 文件

下面的代理从本地文件系统(服务器的本地硬盘)发送HTML文件:

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim body As NotesMIMEEntity

Dim header As NotesMIMEHeader

Dim stream As NotesStream

Set db = s.CurrentDatabase

Set stream = s.CreateStream

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

Set body = doc.CreateMIMEEntity

Set header = body.CreateHeader("Subject")

Call header.SetHeaderVal("HTML message") ' this is the subject

Set header = body.CreateHeader("SendTo")

Call header.SetHeaderVal("user@us.ibm.com")

'The file named below is located on the Domino server because the scheduled agent runs on the Domino server

Call stream.Open("c:\newsletter.html")

Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)

Call doc.Send(False)

s.ConvertMIME = True ' Restore conversion


使用动态HTML内容

下面的代理动态的生成HTML文件。可以给HTML文件加入动态内容。

注意stream.WriteText方法使用管道( | )字符作为分隔符而不是引号。这种用法就可以把引号直接放在字符串前面。同时WriteText与线是分离的,易于用户阅读。不过如果需要它们也能被连接在一起。

示例代码包括换行符,使得消息的源代码具有正确的格式。这一点很重要,因为很多防垃圾邮件系统会过滤掉格式有误的MIME信息

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim body As NotesMIMEEntity

Dim header As NotesMIMEHeader

Dim stream As NotesStream

Set db = s.CurrentDatabase

Set stream = s.CreateStream

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

Set body = doc.CreateMIMEEntity

Set header = body.CreateHeader("Subject")

Call header.SetHeaderVal("HTML message")

Set header = body.CreateHeader("To")

Call header.SetHeaderVal("user@us.ibm.com")

Call stream.writetext(|<HTML>|)

Call stream.writetext(|<body bgcolor="blue" text="white">|)

Call stream.writetext(|<table border="2">|)

Call stream.writetext(|<tr>|)

Call stream.writetext(|<td>Hello World!</td>|)

Call stream.writetext(|</tr>|)

Call stream.writetext(|</table>|)

user$ = s.CommonUserName 'if scheduled agent this returns the name of the server

'Below uses the ampersand (&) to concatenate user$

Call stream.writetext(|<br><font size="+5" color="red">| & user$ &|</font>|)

Call stream.writetext(|</body>|)

Call stream.writetext(|</html>|)

Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)

Call doc.Send(False)

s.ConvertMIME = True ' Restore conversion - very important


提示主题,姓名和HTML文件

Sub Initialize

Dim uiwk As New NotesUIWorkspace

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim body As NotesMIMEEntity

Dim header As NotesMIMEHeader

Dim stream As NotesStream

Dim vname As Variant

Dim vfile As Variant

Dim ssubject As String

Dim snames As String

Dim sfile As String

ssubject = CStr(InputBox("Please enter a subject","Subject input dialog"))

vname = uiwk.PickListStrings(PICKLIST_NAMES,True)

vfile = uiwk.OpenFileDialog(False,"Please select the HTML file to import")

ForAll names In vname

Set db = s.CurrentDatabase

Set stream = s.CreateStream

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

Set body = doc.CreateMIMEEntity

Set header = body.CreateHeader("Subject")

Call stream.Open(vfile(0))

Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)

Call header.SetHeaderVal(subject) ' this is the subject

Set header = body.CreateHeader("SendTo") 'alternatively you can use "BlindCopyTo" instead of "SendTo"

Call header.SetHeaderVal(names)

Call doc.Send(False)

s.ConvertMIME = True ' Restore conversion

End ForAll

End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: