我有很少的Lotus Script或Notes / Domino知识,但我有一个程序,很久以前从某个地方复制过,允许我通过VBA发送电子邮件。我通常只将此用于内部通知,其中格式化并不重要。
我现在想用它来发送外部电子邮件到客户端,公司类型宁愿电子邮件符合我们的风格指南(基本上是无衬线字体)。
我正要告诉他们代码只适用于纯文本,但后来我注意到例程确实引用了某种CREATERICHTEXTITEM对象。这是否意味着我可以在传递给邮件例程之后对正文文本字符串应用某种格式?除了坚持我们宝贵的品牌价值之外,对于我来说,突出电子邮件中的某些段落对我来说非常方便。
我已经深入了解'网络,看看这个代码是否可以调整,但不熟悉Notes的对象模型,以及在线Notes资源似乎反映了应用程序自身的迟钝,这意味着我没有'走得很远。
代码:
Sub sendEmail(EmailSubject As String, EMailSendTo As String, EMailBody As String, MailServer as String) Dim objNotesSession As Object Dim objNotesMailFile As Object Dim objNotesDocument As Object Dim objNotesField As Object Dim sendmail As Boolean 'added for integration into reporting tool Dim dbString As String dbString = "mail\" & Application.UserName & ".nsf" On Error GoTo SendMailError 'Establish Connection to Notes Set objNotesSession = CreateObject("Notes.NotesSession") On Error Resume Next 'Establish Connection to Mail File Set objNotesMailFile = objNotesSession.GETDATABASE(MailServer, dbString) 'Open Mail objNotesMailFile.OPENMAIL On Error GoTo 0 'Create New Memo Set objNotesDocument = objNotesMailFile.createdocument Dim oWorkSpace As Object, oUIdoc As Object Set oWorkSpace = CreateObject("Notes.NotesUIWorkspace") Set oUIdoc = oWorkSpace.CurrentDocument 'Create 'Subject Field' Set objNotesField = objNotesDocument.APPENDITEMVALUE("Subject", EmailSubject) 'Create 'Send To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("SendTo", EMailSendTo) 'Create 'Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("CopyTo", EMailCCTo) 'Create 'Blind Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("BlindCopyTo", EMailBCCTo) 'Create 'Body' of memo Set objNotesField = objNotesDocument.CREATERICHTEXTITEM("Body") With objNotesField .APPENDTEXT emailBody .ADDNEWLINE 1 End With 'Send the e-mail Call objNotesDocument.Save(True, False, False) objNotesDocument.SaveMessageOnSend = True 'objNotesDocument.Save objNotesDocument.Send (0) 'Release storage Set objNotesSession = Nothing Set objNotesMailFile = Nothing Set objNotesDocument = Nothing Set objNotesField = Nothing 'Set return code sendmail = True Exit Sub SendMailError: Dim Msg Msg = "Error # " & Str(Err.Number) & " was generated by " _ & Err.Source & Chr(13) & Err.Description MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext sendmail = False End Sub
答案 0 :(得分:10)
简短的回答是肯定的。答案很长很痛苦。在Notes中没有任何很好的类可以操作富文本项。然而,您可以研究的一些是NotesRichTextStyle,NotesRichTextParagraphStyle和NotesRichTextTable,仅举几例。这些类可帮助您定义一些富文本元素,并以编程方式将它们添加到富文本字段中。
由于您要发送电子邮件,另一种方法是使用NotesMIMEEntity类并使用HTML构建电子邮件(方式更简单)。这是一些示例代码:
Set s = New NotesSession
Dim docMail As NotesDocument
Dim body As NotesMIMEEntity
Dim stream As NotesStream
Set db = s.CurrentDatabase
s.ConvertMIME = False ' Do not convert MIME to rich text
'Create email to be sent
Set docMail = db.CreateDocument
With docMail
.SendTo = SEND TO ADDRESS
.From = FROM ADDRESS
.Principal = FROM ADDRESS
.ReplyTo = REPLY TO ADDRESS
.Subject = SUBJECT
.Form = "Memo"
End With
Set stream = s.CreateStream
Set body = docMail.CreateMIMEEntity
Call stream.WriteText ("YOUR HTML CODE GOES HERE")
'ENC_IDENTITY_8BIT used because of technote found on notes.net
'http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/aeedaf28e47546ad85256f6a000a4b48?OpenDocument
Call body.SetContentFromText (stream, "text/html;charset=iso-8859-1",ENC_IDENTITY_8BIT)
Call docMail.Send(False)
Set docMail = Nothing
Set body = Nothing
Set stream = Nothing
s.ConvertMIME = True ' Restore conversion
基本上,您需要在NotesSession中关闭ConvertMIME设置。然后创建一个新文档,设置邮件属性等。该部分已经在您的VBA代码中。接下来,创建MIMEEntity和一个流来保存HTML文本。最后在MIMEEntity对象上调用SetContentFromText方法。然后发送电子邮件。请注意,最后一个调用是为NotesSession重新打开ConvertMIME功能。
我不确定这是否都可以通过COM工作,但它在Notes中的LotusScript代理中运行良好。
我希望这有帮助!
答案 1 :(得分:0)
我喜欢它的方式。但是,我不得不改变
dbString = "mail\" & Application.UserName & ".nsf"
至
dbString = "mail\" & Application.CurrentUser & ".nsf"
第一条评论:我希望在发送时不必打开Lotus Notes。
第二条评论:我希望我可以更改电子邮件的来源(例如,如果我向50人发送报告,我希望它来自通用地址,而不是我的工作地址)
答案 2 :(得分:0)
要更改其来源,请创建通用Notes标识(例如,自动发送/您的公司)并使用该ID保存代理。