logo
small logo
  • Products
  • Comprar
  • Suporte
  • About
  • Customer panel Suporte
    • en
    • de
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to make the mailing of the report to Email from the database in a WinForms application
  • Como executar um aplicativo com FastReport .NET no Docker sob Linux

    17 de novembro de 2021

    Docker é uma plataforma de software para desenvolvimento rápido, testes e implantação de aplicativos. Com

    read more
  • Toilet paper printing

    30 de março de 2020

    Gentlemen jokes aside! Today we are talking about toilet paper. This essential hygiene product was

    read more
  • FastReport .NET packages e .NET 5.0

    17 de dezembro de 2020

    UPD: aplica-se às versões do FastReport.net antes de 2022.2. Pacotes de licença já estão disponíveis

    read more
  • Create a new report with code VB.NET

    17 de setembro de 2020

    Speaking of the .Net framework, we usually imagine the #C programming language. Simply because the

    read more
  • The Event of ExportParameters in WebReport.Report

    5 de outubro de 2020

    In FastReport 2020.1 we have added the ability to change export parameters. To do this,

    read more

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

7 de março de 2019

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.

about product comprar
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Comprar
  • Download
  • Documentação
  • Depoimentos
  • Como desinstalar nossos produtos
  • FAQ
  • Tutorial Video
  • Forum
  • Support SLA
  • Articles
  • Our News
  • Informação sobre nós
  • Parceiros
  • Extended licensing
  • Contatos

© 1998-2023 by Fast Reports Inc.

  • Privacidade

Trustpilot