0

I have an SSIS package which has a script task that sends emails. When the package is executed locally from Visual Studio, the email is successfully sent. However when I deploy the package through SSIS catalog, create and execute the job, the job runs successfully but I am not receiving any mail. Also when executing the package from under the Integration Services Catalog, it executes without any issues, still no email is sent. I have created a proxy account that uses credential, I guess there might be a missing link or setup error somewhere within these steps. Does anyone know how the Proxy account principals should be set? Or could at all be related to this? I have already tried these:

enter image description here

I have created a c# Console app, just for the email sending part, and when executing that on the same server, it sends the email without any issues.


Additional info to my problem:

  • So initially I also had the Task Version issue (SSIS: script task (vs15) not work when deploy on sql server 2014), but after redeploying it with correct ISDeploymentWizard.exe, then this issue was solved.
  • I’m also using Office365, but 2FA is turned off, and email was sent without any issues when executing the script task from Visual Studio (so when the package execution failed, it correctly went to Event Handlers, executed the script task, and mail was sent).
  • I have proxy account configured to execute the SSIS package, and it works for other packages/steps.
  • I have the following script behind the Script Task:
String SendMailFrom = Dts.Variables["$Project::Sender"].Value.ToString();
String SendMailFromPassword = Dts.Variables["$Project::SenderPassword"].GetSensitiveValue().ToString();
String SendMailTo = Dts.Variables["$Project::ToMail"].Value.ToString();
String SMTPServerName = Dts.Variables["$Project::SMTPServerName"].Value.ToString();
int SMTPServerPort = int.Parse(Dts.Variables["$Project::Port"].Value.ToString());
String PackageName = Dts.Variables["System::PackageName"].Value.ToString();
String ErrorDescription = Dts.Variables["System::ErrorDescription"].Value.ToString();
String ErrorCode = Dts.Variables["System::ErrorCode"].Value.ToString();
String SourceName = Dts.Variables["System::SourceName"].Value.ToString();

String Subject = PackageName + " Package Failed On " + DateTime.Now.ToString();
String Message = PackageName + " Package Failed On " + DateTime.Now.ToString();

try

    MailMessage email = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient(SMTPServerName);
    // START
    email.From = new MailAddress(SendMailFrom);
    email.To.Add(SendMailTo);
    email.Subject = Subject;
    email.Body = Message;
    //END

    SmtpServer.Port = SMTPServerPort;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, SendMailFromPassword);
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(email);
}
catch (Exception ex)
{
}

Dts.TaskResult = (int)ScriptResults.Success; 
Filburt
  • 17,626
  • 12
  • 64
  • 115
Erika_h
  • 3
  • 3
  • Please show a [mcve] of your script task code. – Filburt Oct 02 '21 at 17:04
  • See [this](https://stackoverflow.com/questions/48722170/ssis-email-not-sent-from-deployed-package). Also, is Database mail setup correctly. And see also [this post](https://stackoverflow.com/questions/13933803/ssis-package-execution-succeed-but-no-mail-sent) – Peter Smith Oct 02 '21 at 18:57
  • Thanks for your feedback, I have added additional info to the description – Erika_h Oct 04 '21 at 10:38
  • 1
    You are swallowing all exceptions with that empty `catch` - either re-throw exceptions or log them as SSIS process errors or remove the `try-catch` altogether. You need to find out what exactly is going wrong and the empty `catch` currently hides this away. – Filburt Oct 05 '21 at 08:44

2 Answers2

0

I wrote an article about that.

If you are using Gmail, Outlook365 or any other SMTP server that requires 2FA you are not gonna make it till you turn off 2FA for that e-mail address.

At the bottom of the same article you can find a query that you can use to setup a new e-mail address.

EDIT:

Then use SSIS to send an e-mail using the SQL Server account you have successfully created:

https://www.sqlshack.com/ssis-execute-t-sql-statement-task-vs-execute-sql-task/

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'Your_Profile_Name',
     @recipients = 'Use a valid e-mail address',
     @body = 'The database mail configuration was completed successfully.',
     @subject = 'Automated Success Message';
GO

EDIT 2:

Apparently there eare ways to add a variable inside a T-SQL Statement Task:

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • Thanks for your feedback, I have added additional info to the description – Erika_h Oct 04 '21 at 10:38
  • Have you tried to use the query to manually create the e-mail and then test the e-mail? Testing SSIS package is the next step but first you have to test if SQL Server can actually send the e-mail from that e-mail account – Francesco Mantovani Oct 04 '21 at 11:03
  • I have configured the Database Mail with the same account, and it could send emails. – Erika_h Oct 05 '21 at 12:09
  • Well, now if the e-mail account works fine on SQL Server there is no need to input a new e-mail account with SSIS. Just trig the one you already have setup and works – Francesco Mantovani Oct 05 '21 at 12:25
  • Can you give me an example how to do that? Still need to use Script Task? – Erika_h Oct 05 '21 at 13:10
  • Done, I have updated my reply – Francesco Mantovani Oct 05 '21 at 13:31
  • Thanks, the command itself is working in SSMS, now I'm configuring the SSIS task, however I do not find how to use System Variables in the message body in T-SQL Statement Task. Is that possible? Like the 'System::PackageName' or 'System::ErrorDescription'. – Erika_h Oct 05 '21 at 14:37
  • Not sure... but why don't you just send the e-mail from the GUI? https://stackoverflow.com/questions/36267307/how-to-use-send-email-task-in-ssis-package – Francesco Mantovani Oct 05 '21 at 14:44
  • As far as I know Send Mail task requires windows authentication based SMTP. This is not the case here, that is the reason I have chosen Script Task approach. – Erika_h Oct 07 '21 at 06:59
  • I have updated the reply – Francesco Mantovani Oct 07 '21 at 07:07
0

Thanks to Francesco, finally I have chosen the parameterized "Execute SQL Task" approach with Database Mail configured, and with the script proposed:

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'Your_Profile_Name',
     @recipients = 'Use a valid e-mail address',
     @body = 'The database mail configuration was completed successfully.',
     @subject = 'Automated Success Message';
GO
Erika_h
  • 3
  • 3