Search code examples
sql-serverssisssis-2012

Implement Send Mail Task with multiple tries in case if its failed to send mail


I have implemented a Send Mail Task in my package which sends a mail notification on Success or Failure. The send mail task fails sometimes due to the below error.

Task failed: Send Mail Task with Success
Error Code: -1073548540
ErrorMessage: An error occurred with the following error message: "Failure sending mail. 
System.IO.IOException: Unable to read data from the transport connection: 
An existing connection was forcibly closed by the remote host.  
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host"

I have reported the issue to network admin but they suggested the following.

The errors you are receiving from Mailhub can happen occasionally when trying to open a connection.  
The only way to resolve this issue is to force multiple retries.  If you can, please try to code in ~3-4 retries in your app.

I am sure that it can be done through a script task. I am not sure whether I can implement the multiple tries in case of failure using send mail task.

I have already implemented 20 plus packages with send mail task. I try to implement this approach with minimal change.

I tried the with SQL Server Agent job step configuration, the user has the option of configuring the Retry attempts and the Retry intervals but it runs the whole package on failure which is not suitable for my scenario. I have to run only send mail task alone in case if it failed to send email with multiple tries.


Solution

  • You mentioned C# option:

    here is the logic you are looking for:

    int retryCount = 0;
    
    retry:
    
    try
    {
         [Build and send your email]
    }
    catch
    {
         retryCount++;
         if(retryCount<4) goto retry; //will try 4 times no matter what caused it to fall in to the catch
    }