Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 595
  • Last Modified:

Send HTML Format with Email Object

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("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""  'change to your outbound smtp server
    CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    CON.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 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
1 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.
Bryan ButlerCommented:
Or how about:

Dim MyApp = CreateObject("Outlook.Application")
Dim MyItem = MyApp.CreateItem(0) 'olMailItem
With MyItem
                .To = "email@email.net; dave@dave.com"
                .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")
sharpapproachAuthor Commented:
Worked perfectly!  Thanks!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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