下面的示例演示了如何通过 e-mail 自动发送报表。 要完成此任务,首先报表应被 导出 为可用的格式之一。 在本例中,报表被导出为 PDF,因为这种格式提供了最佳的输出质量 (PDF 结果尽可能地接近于报表的打印结果)。
注意,使用 System.Net.Mail.MailMessage 和 System.Net.Mail.SmtpClient 对象以程序方式发送报表。 在真实的应用程序中,需要为这些类指定自定义设置,并人工调整所有适当的选项 (例如 发件人、收件人、邮件主题、邮件内容、SMTP 服务器 等等)。
C# | 复制代码 |
---|---|
using System; using System.IO; using System.Net.Mail; // ... private void button1_Click(object sender, EventArgs e) { try { // Create a new report. XtraReport1 report = new XtraReport1(); // Create a new memory stream and export the report into it as PDF. MemoryStream mem = new MemoryStream(); report.ExportToPdf(mem); // Create a new attachment and put the PDF report into it. mem.Seek(0, System.IO.SeekOrigin.Begin); Attachment att = new Attachment(mem, "TestReport.pdf", "application/pdf"); // Create a new message and attach the PDF report to it. MailMessage mail = new MailMessage(); mail.Attachments.Add(att); // Specify sender and recipient options for the e-mail message. mail.From = new MailAddress("someone@somewhere.com", "Someone"); mail.To.Add(new MailAddress(report.ExportOptions.Email.RecipientAddress, report.ExportOptions.Email.RecipientName)); // Specify other e-mail options. mail.Subject = report.ExportOptions.Email.Subject; mail.Body = "This is a test e-mail message sent by an application."; // Send the e-mail message via the specified SMTP server. SmtpClient smtp = new SmtpClient("smtp.somewhere.com"); smtp.Send(mail); // Close the memory stream. mem.Close(); mem.Flush(); } catch (Exception ex) { MessageBox.Show(this, "Error sending a report.\n" + ex.ToString()); } } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.IO Imports System.Net.Mail ' ... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Try ' Create a new report. Dim Report As New XtraReport1() ' Create a new memory stream and export the report into it as PDF. Dim Mem As New MemoryStream() Report.ExportToPdf(Mem) ' Create a new attachment and put the PDF report into it. Mem.Seek(0, System.IO.SeekOrigin.Begin) Dim Att = New Attachment(Mem, "TestReport.pdf", "application/pdf") ' Create a new message and attach the PDF report to it. Dim Mail As New MailMessage() Mail.Attachments.Add(Att) ' Specify sender and recipient options for the e-mail message. Mail.From = New MailAddress("someone@somewhere.com", "Someone") Mail.To.Add(New MailAddress(Report.ExportOptions.Email.RecipientAddress, _ Report.ExportOptions.Email.RecipientName)) ' Specify other e-mail options. Mail.Subject = Report.ExportOptions.Email.Subject Mail.Body = "This is a test e-mail message sent by an application." ' Send the e-mail message via the specified SMTP server. Dim Smtp = New SmtpClient("smtp.somewhere.com") Smtp.Send(Mail) ' Close the memory stream. Mem.Close() Mem.Flush() Catch ex As Exception MessageBox.Show(Me, "Error sending a report.\n" + ex.ToString()) End Try End Sub |
Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E16。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |