Send HTML Email via CDO

Ever wonder how to send email that looks like a web page? Here is how you do it. Note: You must have CDO for NT referenced for this to work.

'Sends an email to the appropriate person(s).''From = Email address being sent from'SendTo = List of email addresses separated by a semicolon.  Example:'       [email protected]; [email protected]; [email protected]'Subject = Text that summarizes what the email is about'HTMLText = Body of text that is the email'AttachmentPath = Directory in which the attachment resides'Attachment = File to send with the emailSub SendEmailHTML(From As String, SendTo As String, Subject As String, _    HTMLText As String, Optional AttachmentPath As String, _    Optional Attachment As String, Optional CC As String)    Const constRoutine As String = "SendEmailHTML"    Dim strSendTo As String    Dim objSendMail As CDONTS.NewMail    Dim i As Integer    On Error GoTo ErrorHandler    'Do not cause the user a major error, just log the error and keep going        If SendTo = "" Then Exit Sub    Set objSendMail = New CDONTS.NewMail    With objSendMail        On Error Resume Next        .From = From        If CC <> "" Then            .CC = CC        End If        On Error GoTo ErrorHandler        .To = SendTo        .Subject = Subject        .Body = HTMLText        .MailFormat = CdoMailFormats.CdoMailFormatMime        .BodyFormat = CdoBodyFormats.CdoBodyFormatHTML            AttachmentPath = Trim$(AttachmentPath)        If AttachmentPath <> "" Then            If Right$(AttachmentPath, 1) <> "" Then                AttachmentPath = AttachmentPath & ""            End If            .AttachFile (AttachmentPath & Attachment)        End If        .Send    End With    ExitMe:    Set objSendMail = Nothing    Exit SubErrorHandler:    'Send error back    Err.Raise Err.Number, Err.Source, Err.DescriptionEnd Sub


This tip has been brought to you by Pragmatic Software Co. Inc, the creators of Defect Tracker, the tool of choice for tracking functional specifications, test cases and software bugs. Learn more at http://www.DefectTracker.com. Affiliate program also available at http://www.PragmaticSW.com/AffiliateSignup.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: