Send Email as HTML

It seems that using the Send Mail Task, there is no option for me to format my email as HTML. I have all my HTML code in a string and need to format my email to send the string as an HTML email. What would be the best way to approach this

Answer this question

Send Email as HTML

  • Dugrhill

    Hello,

    What is the namespace:

    Imports Microsoft.SqlServer.Dts.Runtime

    I don't seem to have that reference. Equally, what does it do in relation to the html mail message

    Kind regards



  • JayR

    To put my money where my mouth is and show how easy it is to send an HTML mail message by using the Script ask... here we go.

    Create a string variable to contain the HTML message. I named mine "HtmlText" and set its value to:

    <html><body><h1>Testing</h1><p>This is a <b>test</b> message.</p></body></html>

    Make the variable available to the Script task by adding it to the ReadOnlyVariables property on the Script tab of the Script Task Editor.

    Replace the entire contents of the script window with this code, and replace the From and To email addresses and the SMTP host or server name:

    Imports System
    Imports Microsoft.SqlServer.Dts.Runtime
    Imports System.Net.Mail
    Imports System.Net

    Public Class ScriptMain

        Public Sub Main()

            Dim myHtmlMessage As MailMessage
            Dim mySmtpClient As SmtpClient

            myHtmlMessage = New MailMessage("<from>", _
                "<to>, _
                "Test HTML Message", _
                Dts.Variables("HtmlText").Value.ToString)
            myHtmlMessage.IsBodyHtml = True
            mySmtpClient = New SmtpClient("<smtpservername")
            mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
            mySmtpClient.Send(myHtmlMessage)

            Dts.TaskResult = Dts.Results.Success

        End Sub

    End Class

    The Credentials line might not be required in some circumstances. For production use, you would want to add error-handling, of course.

    -Doug


  • Alexey Logachyov

    Hmm, err.., I, no.



  • Seenivasan.Palaniappan

    If in fact the Send Mail task does not support an HTML body, one could easily supply this functionality by using the Script task and the System.Net.Mail namespace. If you passed in your HTML message body in a string variable, it would take only a few lines of code to create a new MailMessage, set .IsBodyHtml = True, create an SmtpClient object, and use the SmtpClient's Send method to send the HTML message.

    -Doug

  • watoli

    D'Scouser wrote:

    Hello,

    What is the namespace:

    Imports Microsoft.SqlServer.Dts.Runtime

    I don't seem to have that reference. Equally, what does it do in relation to the html mail message

    Kind regards

    Just a guess...you're not using SSIS are you

    -Jamie



  • Send Email as HTML