[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1077
  • Last Modified:

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

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
0
creas002
Asked:
creas002
  • 2
2 Solutions
 
Bill-HansonCommented:
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
  Next
  
  ' 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
  Next
  
  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

0
 
Bill-HansonCommented:
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='http://www.google.com'>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("some.user@acme.com", "Test from VB", html, attachments, imageFiles, imageTypes, imageIds, False)
  'Call SendMimeMemo("some.user@acme.com", "Test from VB", "H:\Temp\any_file.html", attachments, imageFiles, imageTypes, imageIds, True)
    
End Sub

Open in new window

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now