Sending Reports via Email

Note: This article requires SpecFlow+ Runner 1.6 or higher.

If you want to send reports via email after they have been generated, you will need to do so from the command line. You will require a command line tool that is capable of sending emails with attachments, such as mailsend, which will be used in this example.

To set up your reports to be sent by email:

  1. Download a command line email tool such as mailsend and install it (in the case of mailsend, just extract the .exe from the archive). You may want to add the installation path to your environment variables so you do not need to specify the path in your batch file.
  2. Configure your profile to generate the report using the desired template. The easiest way to handle reports you want to send via email is to use a static file name for the report (i.e. without a time stamp). To do so:
    1. Open your .srprofile file.
    2. Add a <Report> element to your .srprofile with the desired settings. Use the outputName attribute in the <Template> element to specify the name of the report. This ensures that the report is always generated using the same name. Make sure you don’t use any placeholders in your file name, such as a time stamp!
      By default, the template’s existingFileHandlingStrategy is set to Overwrite, which will overwrite an existing report with the same file name, if present. This ensures that the report’s file name is always the same, making it easier to choose the correct file to be used as the email attachment. Your addition should look similar to the following (don’t forget to specify the correct .cshtml template):
      <Template name="MyTemplate.cshtml" outputName="Report.html" />
  3. Create a batch file that will be used to start your tests and send the email as an attachment. First you need to run your tests from the command line, so add a corresponding command to the batch file, e.g.:
    runtests.cmd MyProfile
  4. Add a second line to the batch file that will send the email, replacing the file name with the path to your report, e.g.:
    mailsend1.19.exe -smtp -t -f -sub Report -M "The latest SpecRun report is attached." -attach Report.html,text/html,a
    The above command line uses the following options:

    • –smpt: Send the email using the server (replace this with your own mail server)
    • –t: Sends the email to
    • –f: Sets the sender to
      Note: Most properly configured email servers will only allow the sender’s address to be from a specific domain, so make sure you use a suitable email address, such as your own corporate email address.
    • –sub: Sends the email with the subject “Report”
    • –M: Sends the email with the message “The latest SpecRun report is attached.”
    • –attach: Attaches the file “Report.html” (replace this with the name of your report). The attachment’s MIME type is set to “text/html” and included as an attachment (‘a’) as opposed to inline.

    Use mailsend’s –help parameter or refer to the documentation for more information on the available options.

You can generate multiple reports, so you may want to generate two versions of the same report (i.e. using the same template), one with a time stamp for archiving purposes, and one without a time stamp that is sent by email. The report generated based on your entries in the <Settings> element always includes a time stamp, so you can use this to generated the reports for archiving purposes, or add another <Report> element that uses the {now} placeholder to add a time stamp to the file name.