Send an email with an attachment.
Sending query results as a CSV email attachment is a common requirement in Activate workflows. This approach uses an in-memory stream to generate and send the data without writing files to disk, making it well suited to automated jobs and secure environments.
This pattern is typically used for exporting data such as assets, users, or entitlements and delivering it directly to stakeholders via email.
Common Use Cases
This approach is commonly used when:
- Exporting asset, user, or entitlement lists on demand
- Sending scheduled reports to business owners or auditors
- Emailing approval or review data generated by Activate workflows
- Avoiding file system access on application or job servers
Using a MemoryStream ensures the data exists only for the lifetime of the operation and does not leave temporary artefacts on disk.
Example: Email a CSV Export from a MemoryStream
The following example assumes that CSV data has already been written to a stream earlier in the workflow, such as from query results.
Implementation Notes
The stream must remain open for the duration of the email send operation.
- The CSV content is held entirely in memory using a MemoryStream.
- Always reset stream.Position to 0 before creating the attachment.
- This approach avoids writing temporary files to disk.
- MailUtilities.SendEmail uses the mail configuration defined within Activate.
This pattern integrates cleanly with Activate jobs, workflows, and scheduled exports where temporary file storage is undesirable or unavailable.