How to make the mailing of the report to Email from the database in a WinForms application

2019-03-07

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Data;
using System.Windows.Forms;
using FastReport;
using FastReport.Utils;
using FastReport.Export.Pdf;
using FastReport.Export.Email;
 
 private void SendReport_Click(object sender, EventArgs e)
 {
 Config.ReportSettings.ShowProgress = false; //Disable progress window
 
 Report report1 = new Report(); //Create new report object
 report1.Load(Environment.CurrentDirectory + "/text.frx"); //Load report
 report1.Prepare(); //Prepare report
 PDFExport pdf = new PDFExport(); //Cteate PDF export
 EmailExport email = new EmailExport(); //Create Email export
 EmailsDataSet ds = new EmailsDataSet();
 EmailsDataSetTableAdapters.CustomerTableAdapter adapter = new EmailsDataSetTableAdapters.CustomerTableAdapter();
 adapter.Fill(ds.Customer);
 DataTable table = ds.Customer;
 
 foreach (DataRow row in table.Rows)
 {
 SendMessage(report1, pdf, email, row["Email"].ToString(), row["Name"].ToString());
 }
 }

 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void SendMessage(Report report, PDFExport pdf, EmailExport email, string recipient, string custName)
 {
 email.Account.Address = AddressFromTxt.Text;
 email.Account.Name = SenderNameTxt.Text;
 email.Account.Host = HostTxt.Text;
 email.Account.Port = Convert.ToInt16(PortTxt.Text);
 email.Account.UserName = UserNameTxt.Text;
 email.Account.Password = PasswordTxt.Text;
 email.Account.MessageTemplate = "Test";
 email.Account.EnableSSL = true;
 email.Address = recipient;
 email.Subject = MailSubjectTxt.Text;
 email.MessageBody = custName is null? MessageTxt.Text : string.Format("Dear, {0}! {1}", custName, MessageTxt.Text);
 email.Export = pdf; //Set export type
 email.SendEmail(report); //Send email
 }

 And for the Send to all button, let's create an OnClick event handler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void Send_to_all_Click(object sender, EventArgs e)
{
Config.ReportSettings.ShowProgress = false; //Disable progress window
Report report1 = new Report(); //Create new report object
report1.Load(Environment.CurrentDirectory + "/text.frx"); //Load report
report1.Prepare(); //Prepare report
PDFExport pdf = new PDFExport(); //Cteate PDF export
EmailExport email = new EmailExport(); //Create Email export
 
string emails = "";
EmailsDataSet ds = new EmailsDataSet();
EmailsDataSetTableAdapters.CustomerTableAdapter adapter = new EmailsDataSetTableAdapters.CustomerTableAdapter();
adapter.Fill(ds.Customer);
DataTable table = ds.Customer;
 
foreach (DataRow row in table.Rows)
{
if (emails == "")
emails = row["Email"].ToString();
else
emails = emails + ", " + row["Email"].ToString();
}
SendMessage(report1, pdf, email, emails, null);
MessageBox.Show(emails);
}

 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.

.NET .NET FastReport FastReport
April 22, 2025

Working with the TfrShellTreeView Component in FastReport VCL

In this article, we will look at the TfrShellTreeView component. It is designed to display file system elements and is partially analogous to the TDirectoryListBox, TDirectoryOutline, and TShellTreeView components.
April 21, 2025

How RFID Tags Work in FastReport VCL

In this article, we'll check out how RFID tags work with the new TfrxDeviceCommand object in FastReport VCL with release 2025.2.
April 08, 2025

How to Set Up a Connection to Apache Ignite in FastReport .NET

In this article, we will explore how to configure a connection to Apache Ignite in FastReport .NET. You will learn the necessary steps to connect the plugin via code and the report designer.
Fast Reports
  • 800-985-8986 (English, US)
  • +31 97 01025-8466 (English, EU)
  • +49 30 56837-3928 (German, DE)
  • +55 19 98147-8148 (Portuguese, BR)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2025 Fast Reports Inc.