Inside Activate
Development
Send an email with an attachment.
3 min
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 using system io; using system net mail; public void sendcsvexportemail(stream csvstream, string recipientemail) { // create the email message mailmessage message = new mailmessage { subject = "asset list export by activate", body = "attached is the csv containing the results of this export " }; // add recipient message to add(new mailaddress(recipientemail)); // ensure the stream is positioned at the beginning csvstream position = 0; // attach the csv stream attachment csvattachment = new attachment( csvstream, "assetlist csv", "text/csv" ); message attachments add(csvattachment); // send the email using activate mail utilities mailutilities sendemail(message); } 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