How to insert a report into the body of a email

2020-07-09

By default, FastReport.Net allows you to send e-mails with an attached report file in one of the available export formats. However, it is sometimes necessary to include the report content in the body of the email. This feature may be useful if you intend to discuss the report content in correspondence. Responses to an email with an attached report file will no longer have that file. Therefore, you will need to take the time to find an email with an attachment in order to view the report and understand what it is about. It can also be useful if you are viewing mail on a mobile device. Downloading a report file and opening it in another application is not very convenient.

In FastReport.Net we can send emails from the code of a user application, which means that we can set up the settings for emails. Unfortunately, an e-mail message can contain only plain text, without pictures and html markup. Therefore, we can use exporting the report to the txt format to insert it into the body of the message.

Of course, the txt export format imposes many limitations - complete absence of graphics, interactive objects and complex markup - only text. Therefore, it should be used only in simple reports with clear data hierarchy and simple markup. The ideal example - simple lists. Let's consider an example of sending a email with a report in the message:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Create export to txt format
FastReport.Export.Text.TextExport text = new FastReport.Export.Text.TextExport();
 
//Execute export to the file or stream
 report.Export(text, "Template.txt");
 
//Create export to email
 FastReport.Export.Email.EmailExport email = new FastReport.Export.Email.EmailExport();
 
//Set email subject
 email.Subject = "Test";
 
//Set message body – reply in text format
 email.MessageBody = File.ReadAllText("Template2.txt");
 
//Set recipient address
 email.Address = "gromozeka@gmail.com";
 
//Set sender address
 email.Account.Address = "gromozeka@yandex.ru";
 
//Set email host
 email.Account.Host = "smtp.yandex.ru";
 
//Set recipient name
 email.Account.Name = "gromozeka";
 
//Set username
 email.Account.UserName = "gromozeka";
 
//Set user password
 email.Account.Password = "******";
 
//Set email server port
 email.Account.Port = 25;
 
//Enable encryption if needed
 email.Account.EnableSSL = true;
 
//Send email
 email.SendEmail(report);

  The mechanism of sending emails to FR.Net implies sending an email with an attached report, so a report template will be attached by default if you have not specified a certain export format for the attached file.

Now let's see what we get in the e-mail. But first, take a look at the original report:

Report template for the email

 There's beautiful frames, gradient headlines and pictures. There's not gonna be any of that in the text view of the report.

This is what the email with the report in the message looks like:

How the report looks in email body

The report is quite recognizable. If it is not important for you to show the appearance of the report, but only to deliver the information, then this method of sending the report in the body of the message will be quite suitable.

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.