Deprecated: Assigning the return value of new by reference is deprecated in /home/protean/public_html/b/wp-includes/cache.php on line 33
Protean » Blog Archive » SSIS - Sending HTML formatted mail

SSIS - Sending HTML formatted mail

As the SSIS send mail component doesn’t have an option to send html formatted messages, I thought it would help to post some code that implements the system.web.mail namespace.

Considering that sending messages is quite a regular task for any organisation, it might even be worth creating this as a standalone package and calling it each time you need to send a mail. Or even better still, writing a custom component instead.

The Rational Guide to Extending SSIS 2005 with Script (Rational Guides)

If you haven’t already you will probably need to add a reference in your script editor to “system.web.dll”

Having done this the following code should allow you to send an email in html format. Obviously to make this dynamic, you would want populate the message, recipient, from and subject variables with your SSIS variables.

Imports System
Imports System.Web.Mail
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

Public Sub Main()
Dim message, subject, from, recipient As String
Dim mailobject As SmtpMail
Dim mailmessage As New MailMessage

message = "This is a test message”
subject = “Test”
from = “test@test.com”
recipient = “me@test.com”

Try
With mailmessage
.BodyFormat = MailFormat.Html
.Body = message
.From = from
.To = recipient
End With
Catch ex As Exception
Events.FireError(1, “Problem creating message: “, ex.Message, “”, 0)
Dts.TaskResult = Dts.Results.Failure
End Try
Try
With mailobject
.SmtpServer = “mail.test.com”
.Send(mailmessage)
End With
Catch ex As Exception
Events.FireError(1, “Problem sending message: “, ex.Message, “”, 0)
Dts.TaskResult = Dts.Results.Failure
End Try

Dts.TaskResult = Dts.Results.Success
End Sub

End Class

This is the simpler (in my view - not much of a .net coder) .Net 1.0 framework namespace for sending email. There is obviously a newer version (there always is just when you get the hang of something!) , the system.net.mail namespace which you can use if you’re on the .net 2.0 framework or above.

Here is a working package that takes the usual email parameters to send an html formatted email this time using the newer system.net.mail namespace.

Happy days

Frank

4 Responses to “SSIS - Sending HTML formatted mail”

  1. Protean » Blog Archive » Writing SQL results to an SSIS string variable Says:

    […] Finally, if you wanted to place these results in an email, you could use the dts variable that you write to in an expression on the send mail task, or alternatively use the script in the previous post to format the results into HTML and send using system.net.mail class […]

  2. planting anemone corms Says:

    planting anemone corms…

    […]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…

  3. boutique design Says:

    boutique design…

    […]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…

  4. direct loan payment plan Says:

    direct loan payment plan…

    […]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…

Leave a Reply