Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用脚本任务在SSIS发送邮件任务中嵌入HTML_Html_Sql Server_Vb.net_Ssis_Etl - Fatal编程技术网

使用脚本任务在SSIS发送邮件任务中嵌入HTML

使用脚本任务在SSIS发送邮件任务中嵌入HTML,html,sql-server,vb.net,ssis,etl,Html,Sql Server,Vb.net,Ssis,Etl,我正在尝试使用Visual Studio 2012从SSIS 2014内部发送html电子邮件。我尝试构建html代码并通过变量传递它,即User::MailBody。我尝试将其分配给一个字符串,即Dim HtmlBody。我还尝试在脚本中构建它,即messagebody。但这些都不起作用,我不断收到以下错误消息: DTS脚本任务在用户代码中遇到异常: 项目名称:ST_f9d4d770db5047e4beada34f28665e6a 调用的目标已引发异常。 位于System.RuntimeMet

我正在尝试使用Visual Studio 2012从SSIS 2014内部发送html电子邮件。我尝试构建html代码并通过变量传递它,即
User::MailBody
。我尝试将其分配给一个字符串,即
Dim HtmlBody
。我还尝试在脚本中构建它,即
messagebody
。但这些都不起作用,我不断收到以下错误消息:

DTS脚本任务在用户代码中遇到异常: 项目名称:ST_f9d4d770db5047e4beada34f28665e6a 调用的目标已引发异常。 位于System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数) 位于System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象obj,对象[]参数,对象[]参数) 在System.Reflection.RuntimeMethodInfo.Invoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性) 位于System.RuntimeType.InvokeMember(字符串名称、BindingFlags BindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams) 位于Microsoft.SqlServer.Dts.Tasks.ScriptTask.vstatasksscriptingengine.ExecuteScript()处

我的脚本的代码如下所示:

#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
#地区“帮助:脚本任务简介”
'脚本任务允许您执行几乎任何可以在中完成的操作
'集成服务控制流上下文中的.Net应用程序。
'展开具有“Help”前缀的其他区域,以获取使用特定方法的示例
'此脚本任务中的集成服务功能。
#末端区域
#区域“进口”
导入系统
导入系统数据
导入系统。数学
导入Microsoft.SqlServer.Dts.Runtime
导入System.Net.Mail
导入系统.Net
#末端区域
'ScriptMain是脚本的入口点类。不要更改名称、属性、,
'或该类的父级。
_
_
部分公共类ScriptMain
继承Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
#区域“帮助:在脚本中使用Integration Services变量和参数”
'要在此脚本中使用变量,请首先确保已将该变量添加到
'ReadOnlyVariables属性中包含的列表或
'此脚本任务的ReadWriteVariables属性,具体取决于
'代码需要写入变量。若要添加变量,请保存此脚本,然后关闭的此实例
,并更新ReadOnlyVariables和
'脚本转换编辑器窗口中的ReadWriteVariables属性。
'要在此脚本中使用参数,请执行相同的步骤。参数总是只读的。
'从变量读取的示例:
'startTime=Dts.Variables(“System::startTime”).Value
'写入变量的示例:
'Dts.Variables(“用户::myStringVariable”).Value=“新值”
'从包参数读取的示例:
'batchId=Dts.Variables(“$Package::batchId”).Value
'从项目参数读取的示例:
'batchId=Dts.Variables(“$Project::batchId”).Value
'从敏感项目参数读取的示例:
'batchId=Dts.Variables(“$Project::batchId”).GetSensitiveValue()
#末端区域
#区域“帮助:从脚本触发Integration Services事件”
'此脚本任务可以出于日志记录目的触发事件。
'触发错误事件的示例:
'Dts.Events.FireError(18,“进程值”、“错误值”、“0”)
'触发信息事件的示例:
'Dts.Events.FireInformation(3,“进程值”,“处理已开始”,“0,再次启动)”
'触发警告事件的示例:
'Dts.Events.FireWarning(14,“进程值”,“未收到输入值”,“0”)
#末端区域
#区域“帮助:在脚本中使用Integration Services连接管理器”
'此脚本任务中可以使用某些类型的连接管理器。参见主题
“以编程方式使用连接管理器”了解详细信息。
'使用ADO.Net连接管理器的示例:
'Dim rawConnection As Object=Dts.Connections(“Sales DB”).AcquireConnection(Dts.Transaction)
'Dim myADONETConnection As SqlConnection=CType(rawConnection,SqlConnection)
' 
'Dts.Connections(“销售数据库”).ReleaseConnection(rawConnection)
'使用文件连接管理器的示例
'Dim rawConnection As Object=Dts.Connections(“Prices.zip”).AcquireConnection(Dts.Transaction)
'Dim filePath As String=CType(原始连接,字符串)
' 
'Dts.Connections(“Prices.zip”).ReleaseConnection(rawConnection)
#末端区域
'在控制流中执行此脚本任务时调用此方法。
'从该方法返回之前,请将Dts.TaskResult的值设置为指示成功或失败。
'要打开帮助,请按F1。
将messagebody设置为字符串
公用分干管()
'将SSIS变量存储在.Net变量中。您可以跳过此步骤,在实际邮件代码中调用SSIS变量
'以减少代码行数。或者您可以用硬编码的值填充这些.Net变量。
'MsgBox(Dts.Variables(“User::EmailAddress”).Value.ToString)
Dim SendMailFrom As String=Dts.Variables(“User::MailFrom”).Value.ToString()
Dim SendMailTo As String=Dts.Variables(“用户::电子邮件地址”).Value.ToString()
Dim SendMailSubject As String=Dts.Variables(“User::MailSubject”).Value.ToString()
Dim SENDMAILLIONATION As String=Dts.Variables(“用户::联络”).Value.ToString()
Dim SendMailAttachment As String=Dts.Variables(“用户::附件”).Value.ToString()
'Dim SendMailBody As String=Dts.Variables(“User::MailBody”).Value.ToString()
'Dim HtmlBody As String=“请验证以下项目。

” messagebody=“请验证以下项目。

” messagebody=messagebody&“诸如此类,诸如此类
” messagebody=messagebody&“” '从SMTP连接管理器获取SMTP服务器。替代方法是使用额外的变量或参数: 'Dim SmtpServer as String=Dts.Variables(“SmtpServer”).V