Sending Reports by E-Mail
This tutorial expands on Showing Reports in PDF Format by allowing the user to send copies of the report (in RTF format) by e-mail.
The tutorial uses the ExportToFile method provided by the C1Report component. Complete the following steps:
1. From the Toolbox, double-click the Button control to add it to the Web form and set its Text property to Send.
2. Add a Label control to the form (place it next to the button control), and set the following properties:
Property |
Value |
(ID) |
_lblStatus |
Visible |
False |
EnableViewState |
False |
This label will be used to provide feedback after an e-mail is sent. It will usually be invisible, so setting the EnableViewState property to False will save us a little bit of work later. You can show it when you send the message and not bother to hide it again (it will become invisible next time the page loads).
3. Double-click the Send button to add an event handler for the Click event. The Code Editor will open with the insertion point placed within the event handler.
4. Insert the following code:
' Show what happened
_lblStatus.Visible = True
Try
' Export the report to RTF file
Dim fileName As String = Page.MapPath("report.rtf")
C1WebReport1.Report.RenderToFile(fileName,
C1.C1Report.FileFormatEnum.RTF)
' Create the message
Dim mm As New System.Web.Mail.MailMessage()
mm.From = "ceo@nwind.com"
mm.To = "joeManager@nwind.com"
mm.Body = "Hi Joe. Here's a report I just made for you."
' Attach the report
Dim att As New
System.Web.Mail.MailAttachment(fileName)
mm.Attachments.Add(att)
' Send the report
System.Web.Mail.SmtpMail.Send(mm)
_lblStatus.Text = "** Your report is in the mail!"
Catch x As Exception
_lblStatus.Text = "Sorry, failed to send because<br>"
+ x.Message
_lblStatus.BackColor = Color.Pink
End Try
• C#
// Show what happened
_lblStatus.Visible = true;
try
{
// Export the report to RTF file
string fileName = Page.MapPath("report.rtf");
C1WebReport1.Report.RenderToFile(fileName, C1.C1Report.FileFormatEnum.RTF);
// Create the message
System.Web.Mail.MailMessage mm = new
System.Web.Mail.MailMessage();
mm.From = "ceo@nwind.com";
mm.To = "joeManager@nwind.com";
mm.Body = "Hi Joe. Here's a report I just made for you.";
// Attach the report
System.Web.Mail.MailAttachment att =
new System.Web.Mail.MailAttachment(fileName);
mm.Attachments.Add(att);
// Send the report
System.Web.Mail.SmtpMail.Send(mm);
_lblStatus.Text = "** Your report is in the mail!";
}
catch (Exception x)
{
_lblStatus.Text = "Sorry, failed to send because<br>"
+ x.Message;
_lblStatus.BackColor = Color.Pink;
}
Run the project and observe the following:
In a few seconds, the browser will come back showing the report. Click the Send button to send the report through e-mail.
|