Send HTML Format with Email Object

Posted on 2011-10-26
Last Modified: 2013-11-10
We send automated emails from a server as items are processed.  I have everything working, except for the HTML issues.  We used to use text verbiage, but they now want them in HTML formats where the user saves a file and we just pass the path in.  When I try this, it just takes the path name as the HTML text.  Any help is greatly appreciated.

Public Function SendEmail(sTo As String, sFrom As String, sCC As String, sSubject As String, sBody As String, sHTMLPath As String)
    Dim Invoices As String
    Dim MAIL, CON
    Set MAIL = CreateObject("CDO.Message")
    Set CON = CreateObject("CDO.Configuration")
    CON.Fields("") = ""  'change to your outbound smtp server
    CON.Fields("") = 25
    CON.Fields("") = 2
    CON.Fields("") = 60
    Set MAIL.Configuration = CON
    MAIL.From = sFrom
    MAIL.To = sTo
    MAIL.CC = sCC
    MAIL.Subject = sSubject
    MAIL.HTMLBody = sHTMLPath
    Set MAIL = Nothing
    Set CON = Nothing

End Function
Question by:sharpapproach
    LVL 9

    Accepted Solution

    The HTMLBody parameter accepts the HTML itself, not a file path. So you have to read the file and assign the content to HTMLBody.

    As :
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(sHTMLPath, ForReading)
    Dim html
    html = objFile.ReadAll

    Open in new window

    Now you can use the html string variable.
    LVL 16

    Expert Comment

    by:Bryan Butler
    Or how about:

    Dim MyApp = CreateObject("Outlook.Application")
    Dim MyItem = MyApp.CreateItem(0) 'olMailItem
    With MyItem
                    .To = ";"
                    .Subject = "Log File: " + strFilename
                    .ReadReceiptRequested = False
                    .HTMLBody = "Log file attached:" + xPath + "\" + strFilename + "<br>Last modified: " + objFile.DateLastModified + "<br><br> System Info: <br>"
    End With

    Dim myAttachments = MyItem.Attachments
    myAttachments.Add(xPath + "\" + strFilename, 1, 1, "Read Me")

    Author Closing Comment

    Worked perfectly!  Thanks!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    allStar challenge 1 27
    endX challenge 2 33
    Paint/Redraw window while dragging 16 46
    wordappend challenge 8 47
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now