Articles FastReport: How to insert a report into the body of a email

FireWind

Завсегдатай
Staff member
Moderator
FastReport: How to insert a report into the body of a email
July 9, 2020 by Dmitriy Fedyashov
[SHOWTOGROUPS=4,20]
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:

Code:
//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.
[/SHOWTOGROUPS]
 
Top