I have an SSIS package that runs an SQL query and exports it to a csv file via a Data Flow Task. After the csv is created, I have a "Script Task" set to connect to an SMTP server and send the csv file as an attachment.
On my local machine the package runs fine, but when I load it into SQL Server Management Studio on the server it doesn't work as expected. SQL Server MS says that the package executed successfully, and the csv file is generated in the location expected. However, the "Script Task" doesn't appear to be executing at all. I've included some statements in the C# script to write to a file for debugging purposes - one for the try/catch exception block and a couple of others for normal execution.
public void Main()
{
string sSubject = "Weekly PAX Test";
string sBody = "Test Message";
int iPriority = 2;
if (SendMail(sSubject, sBody, iPriority))
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
//Fails the Task
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
public bool SendMail(string sSubject, string sMessage, int iPriority)
{
try
{
string sEmailServer = Dts.Variables["User::sEmailServer"].Value.ToString();
string sEmailPort = Dts.Variables["User::sEmailPort"].Value.ToString();
string sEmailUser = Dts.Variables["User::sEmailUser"].Value.ToString();
string sEmailPassword = Dts.Variables["User::sEmailPassword"].Value.ToString();
string sEmailSendTo = Dts.Variables["User::sEmailSendTo"].Value.ToString();
string sEmailSendFrom = Dts.Variables["User::sEmailSendFrom"].Value.ToString();
string sEmailSendFromName = Dts.Variables["User::sEmailSendFromName"].Value.ToString();
string sAttachmentPath = Dts.Variables["User::sAttachmentPath"].Value.ToString();
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(sEmailSendFrom, sEmailSendFromName);
//You can have multiple emails separated by ;
string[] sEmailTo = Regex.Split(sEmailSendTo, ";");
int sEmailServerSMTP = int.Parse(sEmailPort);
smtpClient.Host = sEmailServer;
smtpClient.Port = sEmailServerSMTP;
System.Net.NetworkCredential myCredentials =
new System.Net.NetworkCredential(sEmailUser, sEmailPassword);
smtpClient.Credentials = myCredentials;
message.From = fromAddress;
if (sEmailTo != null)
{
for (int i = 0; i < sEmailTo.Length; ++i)
{
if (sEmailTo[i] != null && sEmailTo[i] != "")
{
message.To.Add(sEmailTo[i]);
}
}
}
switch (iPriority)
{
case 1:
message.Priority = MailPriority.High;
break;
case 3:
message.Priority = MailPriority.Low;
break;
default:
message.Priority = MailPriority.Normal;
break;
}
//You can enable this for Attachments.
//sAttachmentPath is a string variable for the file path.
Attachment myAttachment = new Attachment(sAttachmentPath);
message.Attachments.Add(myAttachment);
message.Subject = sSubject;
message.IsBodyHtml = true;
message.Body = sMessage;
smtpClient.Send(message);
System.IO.File.WriteAllText("C:\\Users\\SQLCLservice\\SQLServerAgent\\file.txt", "Test");
return true;
}
catch (Exception ex)
{
System.IO.File.WriteAllText("C:\\Users\\SQLCLservice\\SQLServerAgent\\ex.txt", ex.ToString());
return false;
}
}
No email is being sent - no files are being written. It's as if the task is not running at all despite the "Successful Execution".
I did notice that the SQL Server Integration Services 11.0 service is running on my local machine but not on the server. However, if I disable this service on my local machine the task still executes.
Am I missing something else? I'm pretty new to SQL Server and I've been working on this problem for days.
EDIT: I'm running SQL Server 2012
EDIT2: I should also mention that I've tried both saving the package with 64-bit runtime set to false and running it in 32-bit mode through the SQL Server Agent.