SQLTeam.com | Weblogs | Forums

Embedding HTML In SSIS Send Mail Task Using Script Task


#1

I'm trying to send an html e-mail from within SSIS 2014 using Visual Studio 2012. I've tried building the html code and passing it via a variable i.e. User::MailBody. I've tried assigning it to a string i.e. Dim HtmlBody. I've also tried building it within the script i.e. messagebody. None of these work though and I keep getting the following error message:

DTS Script Task has encountered an exception in user code: Project Name: ST_f9d4d770db5047e4beada34f28665e6a Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

The code for my script is as follows:

#Region "Help:  Introduction to the script task"
'The Script Task allows you to perform virtually any operation that can be accomplished in
'a .Net application within the context of an Integration Services control flow. 

'Expand the other regions which have "Help" prefixes for examples of specific ways to use
'Integration Services features within this script task.
#End Region

#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Net.Mail
Imports System.Net
#End Region

'ScriptMain is the entry point class of the script.  Do not change the name, attributes,
'or parent of this class.
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

#Region "Help:  Using Integration Services variables and parameters in a script"
'To use a variable in this script, first ensure that the variable has been added to 
'either the list contained in the ReadOnlyVariables property or the list contained in 
'the ReadWriteVariables property of this script task, according to whether or not your
'code needs to write to the variable.  To add the variable, save this script, close this instance of
'Visual Studio, and update the ReadOnlyVariables and 
'ReadWriteVariables properties in the Script Transformation Editor window.
'To use a parameter in this script, follow the same steps. Parameters are always read-only.

'Example of reading from a variable:
' startTime = Dts.Variables("System::StartTime").Value

'Example of writing to a variable:
' Dts.Variables("User::myStringVariable").Value = "new value"

'Example of reading from a package parameter:
' batchId = Dts.Variables("$Package::batchId").Value

'Example of reading from a project parameter:
' batchId = Dts.Variables("$Project::batchId").Value

'Example of reading from a sensitive project parameter:
' batchId = Dts.Variables("$Project::batchId").GetSensitiveValue()
#End Region

#Region "Help:  Firing Integration Services events from a script"
'This script task can fire events for logging purposes.

'Example of firing an error event:
' Dts.Events.FireError(18, "Process Values", "Bad value", "", 0)

'Example of firing an information event:
' Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, fireAgain)

'Example of firing a warning event:
' Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0)
#End Region

#Region "Help:  Using Integration Services connection managers in a script"
'Some types of connection managers can be used in this script task.  See the topic 
'"Working with Connection Managers Programatically" for details.

'Example of using an ADO.Net connection manager:
' Dim rawConnection As Object = Dts.Connections("Sales DB").AcquireConnection(Dts.Transaction)
' Dim myADONETConnection As SqlConnection = CType(rawConnection, SqlConnection)
' <Use the connection in some code here, then release the connection>
' Dts.Connections("Sales DB").ReleaseConnection(rawConnection)

'Example of using a File connection manager
' Dim rawConnection As Object = Dts.Connections("Prices.zip").AcquireConnection(Dts.Transaction)
' Dim filePath As String = CType(rawConnection, String)
' <Use the connection in some code here, then release the connection>
' Dts.Connections("Prices.zip").ReleaseConnection(rawConnection)
#End Region

'This method is called when this script task executes in the control flow.
'Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'To open Help, press F1.
Dim messagebody As String
Public Sub Main()
' Storing SSIS variables in .Net variables. You could skip this step and call the SSIS variables in the actual mail code
' to reduce the number of code lines. Or you could fill these .Net variables with hardcoded values.
'MsgBox(Dts.Variables("User::EmailAddress").Value.ToString)
Dim SendMailFrom As String = Dts.Variables("User::MailFrom").Value.ToString()
Dim SendMailTo As String = Dts.Variables("User::EmailAddress").Value.ToString()
Dim SendMailSubject As String = Dts.Variables("User::MailSubject").Value.ToString()
Dim SendMailLiaison As String = Dts.Variables("User::Liaison").Value.ToString()
Dim SendMailAttachment As String = Dts.Variables("User::Attachment").Value.ToString()
'Dim SendMailBody As String = Dts.Variables("User::MailBody").Value.ToString()
'Dim HtmlBody As String = "<html><body>Please verify the following items.<br><br>"
messagebody = "<html><body>Please verify the following items.<br><br>"
messagebody = messagebody & "<b>Blah, blah<b><br>"
messagebody = messagebody & "</body></html>"

' Get SMTP Server from SMTP Connection Manager. Alternative is to use extra variables or paramters instead:
' Dim SmtpServer as String = Dts.Variables("SmtpServer").Value.ToString();
Dim SmtpServer As String = Dts.Connections("SMTP Connection Manager").Properties("SmtpServer").GetValue(Dts.Connections("SMTP Connection Manager")).ToString()

' Create an email and change the format to HTML
Dim myHtmlFormattedMail As New MailMessage(SendMailFrom, SendMailTo, SendMailSubject, messagebody)
myHtmlFormattedMail.IsBodyHtml = True

' Create a SMTP client to send the email
Dim mySmtpClient As New SmtpClient(SmtpServer)
'mySmtpClient.Port = 2525 ' If you want to use a different portnumber instead of the default. Else remove this line.
mySmtpClient.Send(myHtmlFormattedMail)
Dts.TaskResult = ScriptResults.Success
End Sub

#Region "ScriptResults declaration"
'This enum provides a convenient shorthand within the scope of this class for setting the
'result of the script.

'This code was generated automatically.
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum

#End Region

End Class

#2

You're going to need to trace your script to see where the exception occurs