The code in the script is as below:
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Net.Mail
Imports System.Net
#End Region
Public Sub Main()
Dim htmlMessageTo As String
Dim htmlMessageFrom As String
Dim htmlMessageSubject As String
Dim htmlMessageBody As String
Dim smtpServer As String
htmlMessageTo = Dts.Variables("HtmlEmailTo").Value.ToString() << Fails here with
dtsruntime error The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
htmlMessageFrom = Dts.Variables("HtmlEmailFrom").Value.ToString()
htmlMessageSubject = Dts.Variables("HtmlEmailSubject").Value.ToString()
htmlMessageBody = Dts.Variables("HtmlEmailBody").Value.ToString()
smtpServer = Dts.Variables("HtmlEmailServer").Value.ToString()
SendMailMessage(htmlMessageTo, htmlMessageFrom, htmlMessageSubject, htmlMessageBody, True, smtpServer)
Dts.TaskResult = ScriptResults.Success
End Sub
Private Sub SendMailMessage( _
ByVal SendTo As String, ByVal From As String, _
ByVal Subject As String, ByVal Body As String, _
ByVal IsBodyHtml As Boolean, ByVal Server As String)
Dim htmlMessage As MailMessage
Dim mySmtpClient As SmtpClient
htmlMessage = New MailMessage( _
SendTo, From, Subject, Body)
htmlMessage.IsBodyHtml = IsBodyHtml
mySmtpClient = New SmtpClient(Server)
mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
mySmtpClient.Send(htmlMessage)
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
Private Sub MailMessage(htmlMessageTo As String, htmlMessageFrom As String, htmlMessageSubject As String, htmlMessageBody As String, p5 As Boolean, smtpServer As String)
Throw New NotImplementedException
End Sub
I thought it was maybe to do with not defining the variables in the readonly section the script task, but I put those in and still get the error:
User::HtmlEmailBody,User::HTMLEmailFrom,User::HtmlEmailServer,User::HTMLEmailSubject,User::HTMLEmailTo
Tried deleting and adding the script task and redefining the variables one by one and it still fails on that first section above. I've also tried putting in "User:" into "HtmlEmailTo" i.e. "User::HtmlEmailTo" and still fails.
Thanks
Andrew