Thursday, August 4, 2011

Send an HTML email in Lotus Notes

Below is a fairly straightfoward way to send an HTML email in Lotus Notes. You can place it just about anywhere, but I recommend placing it in an Agent. That will allow you to work against more data than using it strictly inside a document.

Notice "Call stream.WriteText()", which allows us to add our HTML formatting. You'll still have to follow the normal rules of SMTP. For instance, including CSS in your email will require to call your stylesheet.

Set docMail = db.CreateDocument

'// COMPOSE MESSAGE
session.ConvertMIME = False ' Do not convert MIME to rich text
docMail.Form = "Memo"
docMail.Subject = docNotify.emailSubject(0)
docMail.Principal = "Automated Notification"

'// CREATE MAIN MIME MESSAGE
Set stream = session.CreateStream
Set MIMERoot = docMail.CreateMIMEEntity ' message root

'// CREATE CHILD ENTITIES
Set mime = MIMERoot.CreateChildEntity
Set header = MIMERoot.getNthHeader("Content-Type")
Call header.SetHeaderVal("multipart/related")


'// HEADER
Call stream.WriteText(| |) Call stream.WriteText(|

|) Call stream.WriteText(| 
Text | & document.field(0) & |
|) '// BODY Call stream.WriteText(| |) Call stream.WriteText(| | & document.field(0) & | |) Call stream.WriteText(|
|) '// FOOTER Call stream.WriteText(| |) Call stream.WriteText(| |) Call stream.WriteText(|

Text:
|)
Call stream.WriteText(| Database: | & db.Title & |
|)
Call stream.WriteText(| View: | & document.field(0) & |
|) Call mime.SetContentFromText(stream,"text/html",ENC_NONE) Call stream.Close Set header = mime.CreateHeader("Content-ID") Call header.SetHeaderVal("") Call docMail.Send( False, docNotify.notificationList ) '// RESTORE CONVERSION session.ConvertMIME = True


Optional Coding Parameters you may need:


'// OPTIONAL - Create inline image reference
' Set mime = MIMERoot.CreateChildEntity
' Call stream.Open("http://www.myserver.com/applications/mmail.nsf/" & imagename & "?OpenImageResource")
' Call mime.SetContentFromBytes(stream, "image/jpeg", ENC_NONE)
' Call stream.Close
' Call mime.EncodeContent(ENC_BASE64)


Download the code

There is actually a good book out there for learning more about Lotus Notes. It's one of the few:

No comments:

Post a Comment