VbaFin.com

Visual Basic for Financial Professionals
Home      Lotus Notes
The code snippet shows how to send Lotus Notes mail messages with Microsoft Visual Basic with html text and multiple attachments. Lotus Domino Objects library needs to be installed for the macro to work. Late binding must be used.

If you prefer not to reference the Lotus Domino Objects library you would need to either declare the constants used or replace them with their actual values. They are:

  ENC_IDENTITY_8BIT = 1729
  ENC_IDENTITY_BINARY = 1730

Another simple code example how to send Lotus Notes email with Visual Basic can be found on the IBM's support portal.

Click on the image to download additional examples how to display an email message and use Rich Text format instead of HTML. More information about using Lotus Notes COM objects with Visual basic can be found in their redbook article on the subject.

The LotusScript/COM/OLE Classes Programming Guide, Volume 2 provides comprehensive documentation and plenty of examples as well.
Sub SendNotesMailHTML(Recipient, Optional ccRecipient, Optional bccRecipient, _
    Optional Subject As String, Optional ByVal Attachment, _
    Optional BodyText As String, Optional SaveIt As Boolean)
   
    Dim Maildb As Object, MailDoc As Object, Session As Object, Body As Object
    Dim BodyChild As Object, Header As Object, Stream As Object
    Dim UserName As String, MailDbName As String
    Dim a(1 To 1), FileName As String, i As Long

    On Error Goto ErrHdl

    Set Session = CreateObject("Notes.NotesSession")
    Session.ConvertMIME = False
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    Set Maildb = Session.GetDatabase("", MailDbName)
    If Not Maildb.IsOpen Then Maildb.OPENMAIL
    Set MailDoc = Maildb.CreateDocument
    MailDoc.Form = "Memo"
    MailDoc.SendTo = Recipient
    If Not IsMissing(ccRecipient) Then MailDoc.CopyTo = ccRecipient
    If Not IsMissing(bccRecipient) Then MailDoc.BlindCopyTo = bccRecipient
    MailDoc.Subject = Subject
    Set Body = MailDoc.CreateMIMEEntity
    Set BodyChild = Body.CreateChildEntity
    Set Stream = Session.CreateStream
    Stream.WriteText BodyText
    BodyChild.SetContentFromText Stream, "text/html;charset=iso-8859-1", ENC_IDENTITY_8BIT
    Stream.Close
    Stream.Truncate
    If Not IsArray(Attachment) Then
        If Not
IsMissing(Attachment) Then a(1) = Attachment: Attachment = a
    End If
    If IsArray(Attachment) Then
        For
i = LBound(Attachment) To UBound(Attachment)
            FileName = Dir(Attachment(i))
            If Len(FileName) Then
                Set
BodyChild = Body.CreateChildEntity
                Set Header = BodyChild.CreateHeader( "Content-Type")
                Header.SetHeaderVal "multipart/mixed"
                Set Header = BodyChild.CreateHeader( "Content-Disposition")
                Header.SetHeaderVal "attachment; filename=" & Chr(34) & FileName & Chr(34)
                Set Header = BodyChild.CreateHeader( "Content-ID")
                Header.SetHeaderVal FileName
                Set Stream = Session.CreateStream
                If Stream.Open(Attachment(i), "binary") Then
                    AttachmentType = Split(Attachment(i), ".")
                    AttachmentType = "application/" & AttachmentType(UBound(AttachmentType))
                    BodyChild.SetContentFromBytes Stream, AttachmentType, ENC_IDENTITY_BINARY
                End If
            End If
        Next i
    End If

    MailDoc.SaveMessageOnSend = SaveIt
    MailDoc.PostedDate = Now()
    MailDoc.Send 0, Recipient

ErrHdl:
    If Err.Number Then MsgBox "VBA error: " & Err.Description, vbCritical, "Lotus Notes Email"
    On Error Resume Next
    Session.ConvertMIME = True
    Set
Body = Nothing
    Set
BodyChild = Nothing
    Set
Maildb = Nothing
    Set
MailDoc = Nothing
    Set
Header = Nothing
    Set
Session = Nothing
    Set
Stream = Nothing

End Sub