Using KingswaySoft to Send Emails with Attachments Dynamically

25 August 2022
KingswaySoft Team

Sending attachments in an email is a common and regular use case for almost all integrations, such as for notification or status monitoring purposes. It is sometimes crucial to have an email notification to inform us of the successful or failed job status, including any error details that may have occurred during the process. With that being said, we have seen many cases where the summary report is required after each ETL job run. In such situations, the email notification will need to have attached the generated reports, which is the core requirement we are addressing in this blog post. To fulfill this particular requirement, we suggest you use KingswaySoft's SMTP Task, shipped in our SSIS Productivity Pack, which offers those essential email features along with the email attachment capabilities. In order to work with the dynamic nature of the ETL process, you could leverage the parameterization capabilities included in the task component to send email attachments with different file names each time.

There are also cases where the number of attached files can even be different, which would create extra challenges in SSIS ETL parameterization. In such scenarios, just parameterizing the component using an SSIS expression might not be enough, and you may also need to dynamically create the SSIS expression (which is in JSON format) to handle the necessary process. This is where KingswaySoft's SSIS Productivity Pack comes in handy, with its full suite of various transformation and additional SSIS components that can easily help you achieve such a complex ETL process. In this blog post, we will be showing you a demonstration of how to accomplish the above scenarios with ease. The components that we will use for the use case include:

  • Premium File System Source Component
  • JSON Merge Component
  • Premium Derived Column Component
  • SMTP Task

Please note that the above components are all available for download as part of our SSIS Productivity Pack.

To implement this package, we will first use a Data Flow task to create a JSON document containing all the details about the email attachment before having the document written to an SSIS variable. The ETL package would contain an SMTP Task that is connected right after the first data flow task. While walking you through the design, we will show you how to parameterize the SMTP Task component to send multiple attachments in one email. The overall control flow design would look like something below:

Overall.png

Filtering and fetching the attachment files

As a first step, we need to retrieve the required files based on a particular set of conditions. For this example, let's say you would like to get all the files with a .txt extension from a specific folder. To do this, in the Premium File System Source component, set the Source Item Path as a folder location, and choose the Selected Level (Files only) as the Item Selection Mode. Then, provide FileExtention EQUALS .txt as the filtering conditions in the Filter Parameters. Please note that depending on whether you wish to read recursively, you can change the Item Selection Mode as required.

Premium File System Source - General.png

The next step would be to create a JSON structure. This is what we will provide as the SSIS expression for parameterizing the SMTP Task discussed below. In order to know more about how the parameterization of properties works in our components, please refer to the blog post below:


Based on the details above, we need an expression for the SMTP Task. For creating the JSON format, it is required to pass two fields FullPath and NameWithoutExtension to the original expression, as a repeated structure. The JSON for sending multiple files as Attachment in the SMTP Task component is as shown below (Syntax example):

[ { "name": "FileName1", "path": "FilePath1" }, { "name": "FileName1", "path": "FilePath2" }, { "name": "FileName1", "path": "FilePath3" } ]

Premium File System Source - Columns.png

Building the JSON structure

We will use a JSON Merge component to build the JSON format dynamically. Now that you have the JSON Syntax from the above, you can save it in a notepad (as a .JSON file). And then, open the JSON Merge component, and on the Document Designer page, click on Import and choose JSON (Local File).

JSON Merge - Document Designer.png

On the Columns page, map the Name and Path with the values retrieved from the Premium File System Source component in the upstream data flow.

JSON Merge - Columns.png

Once you have done that, on the Output & Error Handling page, you can set a name for the output column or leave it as default (MergedJson). This column will contain the merged JSON structure, as per the document designer, at runtime.

Writing JSON expression to a Variable

Now is the time to write the merged JSON value into a Variable with which you would parameterize the SMTP Task Component Attachments property. For this, you can use a Premium Derived column in the downstream data flow, and the function WriteValueToVariable(), using Replace function to add "\\" for the FullPath.

WriteValueToVariable(@[User::test1],Replace((DT_WSTR, 1000)[MergedJson],"\\","\\\\"))

Premium Derived Column component.png

Configuring the SMTP Task

In order to send an email with multiple Attachment files, you need to configure the SMTP Task, which is available in the Control Flow SSIS toolbox. After dragging and dropping the SMTP Task, connect the above data flow ("Save JSON structure in a variable") to it. Now, in the SMTP Task component, you could configure and set the connection manager, and provide the necessary information, such as SenderName, SenderAddress, Reply To Address, and Recipients on the General page. To learn more about the component and its configuration, please refer to our Online Help Manual below.

And on the content page, set the subject and the Body as required. In the Attachment page, as we mentioned before, you should parameterize it with the variable that would contain the merged JSON value. And to do that, you could click the "fx" button in the component, select the property Attachments from the list of properties, and then click on the ellipses next to the expression. On the new page, drag and drop the variable from the previous data flow and click on the Evaluate Expression to ensure that the value is set correctly. Please note that to pass SSIS validation, you would need to provide a valid JSON expression in the variable at design time. Then, this would be replaced by the actual value from the previous data flow at runtime.

SMTP_Attachments.png

Conclusion

This way, you can easily use KingswaySoft components to dynamically create and assign expressions to simplify such complex SSIS designs. The use case above is just an example, and the potential for using such logic proves they can be powerful tools in many integration scenarios. We hope this has helped.

Archive

December 2024 1 November 2024 3 October 2024 1 September 2024 1 August 2024 2 July 2024 1 June 2024 1 May 2024 1 April 2024 2 March 2024 2 February 2024 2 January 2024 2 December 2023 1 November 2023 1 October 2023 2 August 2023 1 July 2023 2 June 2023 1 May 2023 2 April 2023 1 March 2023 1 February 2023 1 January 2023 2 December 2022 1 November 2022 2 October 2022 2 September 2022 2 August 2022 2 July 2022 3 June 2022 2 May 2022 2 April 2022 3 March 2022 2 February 2022 1 January 2022 2 December 2021 1 October 2021 1 September 2021 2 August 2021 2 July 2021 2 June 2021 1 May 2021 1 April 2021 2 March 2021 2 February 2021 2 January 2021 2 December 2020 2 November 2020 4 October 2020 1 September 2020 3 August 2020 2 July 2020 1 June 2020 2 May 2020 1 April 2020 1 March 2020 1 February 2020 1 January 2020 1 December 2019 1 November 2019 1 October 2019 1 May 2019 1 February 2019 1 December 2018 2 November 2018 1 October 2018 4 September 2018 1 August 2018 1 July 2018 1 June 2018 3 April 2018 3 March 2018 3 February 2018 3 January 2018 2 December 2017 1 April 2017 1 March 2017 7 December 2016 1 November 2016 2 October 2016 1 September 2016 4 August 2016 1 June 2016 1 May 2016 3 April 2016 1 August 2015 1 April 2015 10 August 2014 1 July 2014 1 June 2014 2 May 2014 2 February 2014 1 January 2014 2 October 2013 1 September 2013 2 August 2013 2 June 2013 5 May 2013 2 March 2013 1 February 2013 1 January 2013 1 December 2012 2 November 2012 2 September 2012 2 July 2012 1 May 2012 3 April 2012 2 March 2012 2 January 2012 1

Tags