Sending HTML Email Using SQL Server Integration Services

March 3, 2009 17:33 by dana

SQL Server Integration Services offers an easy way to send email in your packages, but out-of-the-box it only supports sending plain text email. Below is a script that can be used to send HTML email.

 

    '// Example :
    '// SendMail("recipient@theiremail.com, _
    '//          alerts@mycompany.ca, _
    '//          "I can now send HTML Email :)", _
    '//          "<B>Yipee!</B> I can send email messages using HTML formatting from SSIS.", _
    '//          True)
    Private Sub SendMail( _
          ByVal SendTo As String, _
          ByVal From As String, _
          ByVal Subject As String, _
          ByVal Body As String, _
          Optional ByVal IsBodyHtml As Boolean = True, _
          Optional ByVal SMTPServer As String = "localhost", _
          Optional ByVal UserName As String = "", _
          Optional ByVal Password As String = "", _
          Optional ByVal Domain As String = "", _
          Optional ByVal Attachments As String = "")

        Dim oMessage As System.Net.Mail.MailMessage
        Dim mySmtpClient As System.Net.Mail.SmtpClient

        oMessage = New System.Net.Mail.MailMessage(From, SendTo, Subject, Body)
        oMessage.IsBodyHtml = IsBodyHtml
        '//Attachments
        If Not String.IsNullOrEmpty(Attachments) Then
            Dim sFiles() As String
            Dim sFile As String
            sFiles = Split(Attachments, ";")
            For Each sFile In sFiles
                If Not String.IsNullOrEmpty(sFile) Then
                    oMessage.Attachments.Add(New Net.Mail.Attachment(sFile))
                End If
            Next
        End If

        mySmtpClient = New System.Net.Mail.SmtpClient(SMTPServer, 25)
        If UserName = "" Then
            mySmtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
        Else
            mySmtpClient.Credentials = New System.Net.NetworkCredential(UserName, Password, Domain)
        End If
        mySmtpClient.Send(oMessage)
    End Sub