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.
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.RuntimePublic Class ScriptMain
Public Sub Main()
Dim message, subject, from, recipient As String
Dim mailobject As SmtpMail
Dim mailmessage As New MailMessagemessage = "
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 TryDts.TaskResult = Dts.Results.Success
End SubEnd 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

August 14th, 2008 at 10:50 am
[…] 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 […]
January 31st, 2012 at 7:58 pm
planting anemone corms…
[…]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…
February 1st, 2012 at 3:18 pm
boutique design…
[…]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…
February 6th, 2012 at 6:42 am
direct loan payment plan…
[…]Protean » Blog Archive » SSIS - Sending HTML formatted mail[…]…