Reports are an integral part of the workflow, and electronic reports - electronic workflow. One of the main mechanisms for distributing electronic reports is e-mail. Perhaps, all modern report generators have a built-in mail client to be able to send a report directly from the program.
FastReport.Net is no exception. You can send the report in preview mode, or directly from the code of the user application. This is convenient if you send a report to a single recipient. Although it is possible to add multiple recipients, it is not always suitable. For example, when you want to make a newsletter with the name of the user: "Dear, Ivan Ivanovich ...". Therefore, we consider an example of how to send a report to several recipients, whose addresses and names are taken from the database. First, create a database and a table in it. For example, the database acces in mdb format:
Create a WinForms application. Add two buttons and text fields for email settings to the form:
The first button will send the report by name, and the second will send the report to the address list. You will understand how this works later, from the code.
Each field has a default value; if you wish, you can enter a different value.
Create an application data source using the wizard:
Following further, we configure the connection string to the database and select the table:
Add a link to the FastReport library to the project. We also need a file with a report template, which we will send out. Add it to the project. For example, we will use the report text.frx from the Demo folder.
Now create a handler for the Direct email button event:
Here we first create the report object, load the template into it, create export to PDF and Email. The report will be attached to the letter in PDF format.
Next, we create a data source, fill the table with data. We look through the table entries and for each of them we send a letter. Of course, if you have a large mailing list, this method will work slowly. But you can insert in the text of the letter the name of the client.
As you have noticed, we have taken out sending the letter to a separate method - SendMessage:
And for the Send to all button, let's create an OnClick event handler:
Unlike the previous code, here in the cycle we add addresses to send to a variable, then pass it to the message sending method. This method works much faster than the previous one, especially if the mailing list is large. Judge for yourself, here you send one letter, instead of a set.
Thus, the task of sending a report is reduced to exporting to email and sending a letter to each address from the database.