Link to home
Start Free TrialLog in
Avatar of fcoit
fcoitFlag for United States of America

asked on

How to resolve Mapi.Message not supporting HTMLBody from MS Access

Hello experts,

I am trying to use Mapi to send email from MS Access.  I know there other ways to achieve this which would resolve my problem of dealing HTML.  However, there is a con so here is the dilema.  I need to use Mapi because it is the only way I found it gets around the profile resolution with outlook.  Everything works fine except for the HTML format.  How can this be resolved?  Thank you.
Private Sub SendEmail(strEmailAdd As String, strFrom As String, strMessage As String, _
                      strSubject As String, strCc As Variant, strBcc As Variant)
On Error GoTo errHandler
 
   Dim oSession As MAPI.Session
   Dim MsgNew As MAPI.Message  'uses early binding
   Dim Recip As MAPI.Recipient
   Dim RecipCC As MAPI.Recipient
   Dim RecipBCC As MAPI.Recipient
   Dim AddEntries As MAPI.AddressEntries
   Dim OnBehalfSender As MAPI.AddressEntry
   Dim aCc() As String
   Dim aBcc() As String
   Dim strNTUser As String
   Dim i As Integer
 
   Set oSession = CreateObject("mapi.session")
   strNTUser = Environ("UserName")
   oSession.Logon profileName:=strNTUser    'use existing session
 
   'create new message
   Set MsgNew = oSession.Outbox.Messages.Add
 
   'set on behalf sender
   Set AddEntries = oSession.AddressLists(1).AddressEntries
   AddEntries.Filter = Nothing    'reset
   'TODO: Change on behalf user name
   AddEntries.Filter.Name = strFrom
   Set OnBehalfSender = AddEntries.GetFirst
   Set MsgNew.Sender = OnBehalfSender  'set on behalf address
   Set MsgNew.Sender = oSession.CurrentUser  'optional, the actual sender
 
   'set message recipient
   'TODO: Change recipient name
   Set Recip = MsgNew.Recipients.Add(strEmailAdd, , 1)
   Recip.Resolve
 
 
   'set message recipient
   'TODO: Change recipient name
    aCc = Split(strCc, ";")
    For i = 0 To UBound(aCc)
        Set RecipCC = MsgNew.Recipients.Add(aCc(i), , 2)
        RecipCC.Resolve
    Next i
 
   'set message recipient
   'TODO: Change recipient name
    aBcc = Split(strBcc, ";")
    For i = 0 To UBound(aBcc)
        Set RecipBCC = MsgNew.Recipients.Add(aBcc(i), , 3)
        RecipBCC.Resolve
    Next i
 
   'set other message properties and send
   With MsgNew
      '.Text = strMessage
      .Text.HTMLBody = strMessage
      .Subject = strSubject
      .Update    'optional, leaves unsent mail in Outbox if Send fails
      .Send
   End With
   
   
 
   'release objects
   Set MsgNew = Nothing
   Set OnBehalfSender = Nothing
   Set Recip = Nothing
   Set RecipCC = Nothing
   Set RecipBCC = Nothing
   Set AddEntries = Nothing
   oSession.Logoff
   Set oSession = Nothing
    
ExitHere:
    Exit Sub
 
errHandler:
    Select Case Err
        Case Else
            MsgBox "Error Number: " & Err.Number & vbNewLine & "Description: " & Err.Description, vbCritical, "Error"
        GoTo ExitHere
    End Select
End Sub

Open in new window

Avatar of Chuck Wood
Chuck Wood
Flag of United States of America image

I had to use .Display in Outlook before adding the body to get the HTML to work. Perhaps there is something similar in MAPI?

-Chuck
Avatar of fcoit

ASKER

cwood-wm-com,

I found this but I do not know how to incorporate this into my code.

oMailItem.BodyFormat = OlBodyFormat.olFormatHTML

Thank you for your input.

As far as the Display method is not available.
ASKER CERTIFIED SOLUTION
Avatar of Chuck Wood
Chuck Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fcoit

ASKER

Thanks again.

I was hoping to stay away from Outlook.  The problem with Outlook is that if a session has not been started the user profile will not be resolved and that will not work.  So the user must open Outlook before running the code.