Using VB 6.0 to send email via Lotus Notes session - Need to be able to Embed viewable HTML

Posted on 2009-04-23
Last Modified: 2013-12-18
I've spent hours reading various threads on using RichText and Mime messages, and sending attachements, but I have not found a way to produce an email message that is HTML viewable in LOTUS Notes.  

I need to be able to send very long URL links and specify an abbreviated link name to click without seeing the actual URL.

Please Help
Question by:creas002
    LVL 22

    Accepted Solution

    Then MIME is the answer.

    Here is a function that you can just paste into your project.

    Make sure that you have the "Lotus Domino Objects" library included in your project references.  If you need a late-bound example, let me know.

    There are two procedures here.  The first one does all the work.  The second one simply reads text from a file and is required by the first one.

    I'll post a usage example next...
    Sub SendMimeMemo(sendto As Variant, ByVal subject As String, ByVal html As String, attachments() As String, imageFiles() As String, imageTypes() As String, imageIds() As String, ByVal fromFile As Boolean)
      ' * Sends an html formatted email.
      ' * This function supports file attachments and inline images.
      ' * The html source may be passed in as a string or may be read from a file.
      ' * @param sendto A string (or array of strings) containing the email recipient(s).
      ' * @param subject A string containing the email subject.
      ' * @param html A string containing html for the email body or the path to an html file.
      ' * @param attachments An array of strings containing the full path to files to attach to this email.
      ' * @param imageFiles An array of strings containing the full path to any inline images.
      ' * @param imageTypes An array of strings containing the mime type for the images in the imageFiles parameter ("image/jpg", "image/gif").
      ' * @param imageIds An array of strings containing the content id (CID) for the images in the imageFiles parameter.
      ' * @param fromFile Set this parameter to True if the html parameter points to a file.
      ' */
      Dim sess As New NotesSession
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      Dim stream As NotesStream
      Dim mimeBody As NotesMIMEEntity, mimeHtml As NotesMIMEEntity
      Dim mimeFile As NotesMIMEEntity, mimeImage As NotesMIMEEntity
      Dim mimeHeader As NotesMIMEHeader
      Dim mailServer As String, mailFile As String
      Dim convertMime As Boolean
      ' Init Notes session
      Call sess.Initialize("")
      ' Open user's email
      mailServer = sess.GetEnvironmentString("MailServer", True)
      mailFile = sess.GetEnvironmentString("MailFile", True)
      Set db = sess.GetDatabase(mailServer, mailFile)
      ' Create an email doc
      Set doc = db.CreateDocument
      Call doc.ReplaceItemValue("Form", "Memo")
      Call doc.ReplaceItemValue("SendTo", sendto)
      Call doc.ReplaceItemValue("Subject", subject)
      doc.SaveMessageOnSend = True
      ' add the body as a mime html part
      convertMime = sess.convertMime
      sess.convertMime = False
      Set stream = sess.CreateStream()
      If (fromFile) Then html = FileRead(html)
      stream.WriteText (html)
      Set mimeBody = doc.CreateMIMEEntity("Body")
      Set mimeHtml = mimeBody.CreateChildEntity
      Call mimeHtml.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)
      Call stream.Close
      ' add file attachments
      For i = 0 To UBound(attachments)
        Set mimeFile = mimeBody.CreateChildEntity
        Set mimeHeader = mimeFile.CreateHeader("Content-Transfer-Encoding")
        Call mimeHeader.SetHeaderVal("binary")
        Set mimeHeader = mimeFile.CreateHeader("Content-Disposition")
        Call mimeHeader.SetHeaderVal("attachment; filename=" & attachments(i))
        Call stream.Open(attachments(i), "binary")
        Call mimeFile.SetContentFromBytes(stream, "text/plain", ENC_NONE)
        Call mimeFile.EncodeContent(ENC_IDENTITY_8BIT)
        Call stream.Close
      ' add images referenced by cid tags
      For i = 0 To UBound(imageFiles)
        Set mimeImage = mimeBody.CreateChildEntity()
        Set mimeHeader = mimeImage.CreateHeader("Content-ID")
        Call mimeHeader.SetHeaderVal("<" & imageIds(i) & ">")
        Call stream.Open(imageFiles(i))
        Call mimeImage.SetContentFromBytes(stream, imageTypes(i) & "; name=" + imageIds(i), ENC_IDENTITY_BINARY)
        Call stream.Close
      sess.convertMime = convertMime
      Call doc.CloseMIMEEntities(True, "Body")
      Call doc.Send(False)
    End Sub
    Public Function FileRead(ByVal path As String) As String
        ' * Reads an entire file into a string.
        ' * @param path The path to the file to read.
        ' * @return A string containing the contents of the specified file.
        ' */
        Dim fileNum As Integer
        fileNum = FreeFile
        Open path For Input As fileNum
        FileRead = Input$(LOF(fileNum), fileNum)
        Close fileNum
    End Function

    Open in new window

    LVL 22

    Assisted Solution

    As promised, here is a usage example.

    The commented line is an example of using an html file rather than a string.

    Private Sub Test_JPEG_Click()
      Dim html As String
      Dim attachments(0) As String, imageFiles(0) As String, imageTypes(0) As String, imageIds(0) As String
      html = "<div style='font-size: 10pt; font-family: Arial, Helvetica, sans-serif; font-weight: bold;'><img src='cid:image_1.jpg'><br>This is a test!<br><a href=''>Google</a><br><br></div>"
      attachments(0) = "h:\temp\any_file.txt"
      imageFiles(0) = "h:\temp\any_image.jpg"
      imageTypes(0) = "image/jpg"
      imageIds(0) = "image_1.jpg"
      Call SendMimeMemo("", "Test from VB", html, attachments, imageFiles, imageTypes, imageIds, False)
      'Call SendMimeMemo("", "Test from VB", "H:\Temp\any_file.html", attachments, imageFiles, imageTypes, imageIds, True)
    End Sub

    Open in new window


    Author Closing Comment

    Perfect Bill.... I had come across this before but never got it to work until now.  It works very well!!!  Nicely done!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now