segunda-feira, 9 de julho de 2012

How to send HTML formatted mail messages using a LotusScript agent

REF: http://www-01.ibm.com/support/docview.wss?uid=swg21098323

Question

How can you send HTML formatted mail messages using LotusScript?
Prior to the expansion of the NotesMIMEEntity class in Lotus Notes and Lotus Domino Designer 6.x, it was not possible to use a scheduled agent to send mail messages with HTML formatting. When you added HTML code to the body of a mail message, even setting the PassThruHTML NotesRichTextStyle property, Notes sent only the raw HTML code to the recipient without rendering it into HTML.

Answer

New methods and properties in the NotesMIMEEntity class, as well as the addition of the NotesStream class, make it possible to send HTML formatted mail directly from a scheduled agent in Notes/Domino 6.x and later. This functionality is useful for sending HTML newsletters or for responding to users who submit information into a mail-in database. You can create an agent that sends an HTML file stored on the local file system or that creates the HTML dynamically. The agents below demonstrate this functionality.
IMPORTANT NOTE: The following is a sample script, provided only to illustrate one way to approach this issue. In order for this example to perform as intended, the script must be laid out exactly as indicated below. IBM Support will not be able to customize this script for a customer's own configuration.


Using a local HTML file

The following agent sends an HTML file from the local file system (server's hard drive):

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



Using dynamic HTML content
    The following agent builds an HTML file dynamically. It is also possible to add dynamic content into the HTML as demonstrated.

    Notice that the stream.WriteText method uses the pipe ( | ) string delimiter rather than quotation marks. This usage makes it easier to place quotation marks directly into the string. Also, the WriteText lines are separated for easier reading, but they could be concatenated together.

    The example code contains EOL_CRLF characters to make the message source properly formatted. This is important because many spam filters block improperly formatted MIME messages.

    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(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call stream.writetext(|
    Hello World!
    |)
    user$ = s.CommonUserName 'if scheduled agent this returns the name of the server
    'Below uses the ampersand (&) to concatenate user$
    Call stream.writetext(|
    | & user$ &||)
    Call stream.writetext(||)
    Call stream.writetext(||)
    Call body.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_IDENTITY_7BIT)
    Call doc.Send(False)
    s.ConvertMIME = True ' Restore conversion - very important



    Using a prompt for subject, name(s), and HTML file

    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(ssubject) ' 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

    Nenhum comentário:

    Postar um comentário