Wednesday, August 17, 2011

Email Exported Crystal Reports and devexpress XtraReports as pdf from C# Application


Using Devexpress Report to mail pdf:
this Code Snippet automatically send a report via e-mail. To do this, a report should first be
exported into one of the available formats. In this example, a report is exported to PDF, since
this format provides the best output quality (the PDF result is as close to a report's print result
as possible).

To do as we want first export the report to Memory Stream and add the attachment to System.Net.
MailMessage Object.. then provide the credenitials of the smtp server to send email, finally send the
email SmtpClient object.
  
private void SendExportedReportPDFAsEmail_Click(object sender, EventArgs e) 
{
     try
      {
         // Create a new report
.           EmployeeReport report = new EmployeeReport();
          // 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 attachment = 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(attachment);
          // Specify sender and recipient options for the e-mail message. 
         mail.From = new MailAddress("your Email Address", "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.yourdomain.com"); e.g. smtp.gmail.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());
     }
 } 
Using Microsoft Crystal Report:
Here i am using 3 button on for exporting the document to the file system and then attach the
exported document to the email attachment.
public partial class EmployeeReportForm: Form
{
ReportDocument cryRpt;
string pdfFile = "c:\\employeedetails.pdf";
public EmployeeReportForm()
{
InitializeComponent();
}
private void LoadReport_Click(object sender, EventArgs e)
{
      cryRpt = new ReportDocument();
      cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\EmployeeReport.rpt");
      crystalReportViewer1.ReportSource = cryRpt;
      crystalReportViewer1.Refresh();
}
private void ExportReportFileAndEmail_Click(object sender, EventArgs e)
{
   try
   {
            ExportOptions CrExportOptions ;
              DiskFileDestinationOptions CrDiskFileDestinationOptions = 
                                                 new DiskFileDestinationOptions();
              PdfRtfWordFormatOptions CrFormatTypeOptions = 
                                                    new PdfRtfWordFormatOptions(); 
             CrDiskFileDestinationOptions.DiskFileName = pdfFile;
              CrExportOptions = cryRpt.ExportOptions;
              CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
              CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
              CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
              CrExportOptions.FormatOptions = CrFormatTypeOptions;
              cryRpt.Export();
                  sendmail();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.ToString());
             }
         }
private void sendmail()
{
  try {
        SmtpMail.SmtpServer.Insert(0, "your hostname");
        MailMessage Msg = new MailMessage();
        Msg.To = "to address here";
        Msg.From = "from address here";
        Msg.Subject = "Crystal Report Attachment ";
        Msg.Body = "Crystal Report Attachment ";
        Msg.Attachments.Add(new MailAttachment(pdfFile));
        System.Web.Mail.SmtpMail.Send(Msg);
      }
      catch (Exception ex)
{
         MessageBox.Show (ex.ToString());
      }
}
}
}
try to use correct smtp server details for sending email correctly..
if mail failed the use the smtp client credentials as:
smtpClient.Credentials = new System.Net.NetworkCredential("MailUser", "MailPass");  
 smtpClient.UseDefaultCredentials = false;
 

No comments :

Post a Comment