Send Report by Email

This example shows how to send exported reports by Email using the viewer. The report can be exported in different formats: PDF, Excel, HTML, Rich Text, image and many other, the viewer can send this file by Email.

First, you need to add the StiMvcViewer component to the view page. Also you need to pass the StiMvcViewerOptions object to the constructor. In the options, you should set the next actions - GetReport, EmailReport and ViewerEvent. They are located in the Actions options group. To enable the the ability to send Email, you should set the ShowSendEmailButton option in the Toolbar group to true.

@using Stimulsoft.Report.Mvc;

...

@Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions()
{
Actions =
{
GetReport = "GetReport",
EmailReport = "EmailReport",
ViewerEvent = "ViewerEvent"
},
Toolbar =
{
ShowSendEmailButton = true
}
})


In the options above we define several actions, and we need to add it in the controller.

The GetReport action loads the report and returns the answer to the client part of the viewer using the GetReportResult() static method. In the parameters of this method, the report object should be passed.

public ActionResult GetReport()
{
// Create the report object
StiReport report = new StiReport();
report.Load(Server.MapPath("~/Content/Reports/TwoSimpleLists.mrt"));

// Load data from XML file for report template
DataSet data = new DataSet("Demo");
data.ReadXml(Server.MapPath("~/Content/Data/Demo.xml"));

report.RegData(data);

return StiMvcViewer.GetReportResult(report);
}


The ViewerEvent action handles all the viewer events (switching pages, zooming, etc.) and returns the answer to the client using the ViewerEventResult() static method.

public ActionResult ViewerEvent()
{
return StiMvcViewer.ViewerEventResult();
}


The EmailReport action will be invoked when you send a report by Email through menu of the viewer. In this action, you can get the email options object of the StiEmailOptions type. In these options will be passed all the data required for sending the Email. You need to fill the necessary options, such as server address, login, password and other. Also you can change the address of the recipient, email subject and body, if it is important (these options are requested in the viewer dialog).

To prepare the answer for the client you should use the EmailReportResult() static method. In the parameters of this method, the email options object should be passed.

public ActionResult EmailReport()
{
StiEmailOptions options = StiMvcViewer.GetEmailOptions();

options.AddressFrom = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
//options.AddressTo = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
//options.Subject = "Quarterly Report";
//options.Body = "Quarterly report on arrival of the goods.";

options.Host = "smtp.test.com";
//options.Port = 465;
options.UserName = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
options.Password = "************";

return StiMvcViewer.EmailReportResult(options);
}


In the screenshot below you can see the result of the sample code.

Send Report by Email