It is no secret that FastReport .Net allows you to send reports by email. But few people know how to do it by using the code in a custom application. In such way that you wouldn't have to run a report and send it via email manually. The required reports will be automatically sent to a specified e-mail address with help one button, or schedule.
Let's consider the example of sending the report in PDF format by e-mail.
Create WindowsForms application. Add buttons and two text fields. In the first one, we will input recipient's email address, and the second - mail server for outgoing mail.
We will need the following libraries:
1 2 3 |
using FastReport; using FastReport.Export; using FastReport.Utils; |
Create an event handler for the button click:
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 |
private void button1_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 FastReport.Export.Pdf.PDFExport pdf = new FastReport.Export.Pdf.PDFExport(); //Cteate PDF export FastReport.Export.Email.EmailExport email = new FastReport.Export.Email.EmailExport(); //Create Email export //email mailer settings email.Account.Address = "gromozekaster@yandex.ru"; email.Account.Name = "TestUser"; email.Account.Host = textBox2.Text; email.Account.Port = 25; email.Account.UserName = "Gromozekaster"; email.Account.Password = "Password"; email.Account.MessageTemplate = "Test"; email.Account.EnableSSL = true; //email addressee settings email.Address = textBox1.Text; email.Subject = "Test Report"; email.MessageBody = "Test message"; email.Export = pdf; //Set export type email.SendEmail(report1); //Send email } |
First of all I disabled the progress window in the report settings, but you can skip this step. If you do not need it, just do not write this line of code. Then create an instance of a report object and load the report into it. Before the export you need to prepare a report (Prepare), in other words - build it. Since the title of the article contains “PDF”, let's create an object of export to PDF. Likewise we create export to email.
Now we can customize the export settings in the Email. In the Account - the sender's settings. Basically it is the outgoing mail server settings.
Next - set the parameters of the recipient as well as the email address, subject line, message text and export to the desired file type. Here it should be noted that is not necessary to export to PDF, you need only to create an export object. Email export makes exports to the specified format.
If you do not specify Export parameter, then the report will be sent in the FPX format. This file is preview of the report. You can view, print or export the report, but can not edit it.
Finally, we will send a letter using Sendmail method. Be sure to pass the report object to this method.
Considered method will be useful for standard emails, for example, you can arrange to automatically broadcast daily reports to your chief email.