Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

19.09 Exporting to Email

Exporting to Email

Exporting a report to an email creates the report as a separate file and attaches it to an email message. This email message is automatically sent out to the recipient. Table 19-7 shows the properties for exporting to email. Each of these properties relates to the typical settings you find when sending an email message.

Table 19-7. MicrosoftMailDestinationOptions Properties.

Property Description
MailCCList The list of emails to send a carbon copy to.
MailMessage The text portion of the email message.
MailSubject The text subject heading of the email message.
MailToList The email(s) of those receiving the report.
Password The password used when logging on to the email account.
UserName The user name used when logging on to the email account.

Listing 19-4. Send a report as an attachment of an email.
[VB.NET]
Public Sub ExportToEmail(ByVal myReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal MailTo As String, ByVal CCList As String, ByVal Subject As String, ByVal Message As String, ByVal UserName As String, ByVal Password As String)
'Set the destination type to Email
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
'Instantiate an Email options object and set its properties
Dim myOptions As CrystalDecisions.Shared.MicrosoftMailDestinationOptions
myOptions = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions()
myOptions.UserName = UserName
myOptions.Password = Password
myOptions.MailSubject = Subject
myOptions.MailMessage = Message
myOptions.MailToList = MailTo
myOptions.MailCCList = CCList
'Assign the options object to the report
myExportOptions.DestinationOptions = myOptions
myReport.Export(myExportOptions)
End Sub
[C#]
public void ExportToEmail(CrystalDecisions.CrystalReports.Engine.ReportDocument myReport, string MailTo, string CCList, string Subject, string Message, string UserName, string Password)
{
//Set the destination type to Email
CrystalDecisions.Shared.ExportOptions myExportOptions = new CrystalDecisions.Shared.ExportOptions();
myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
//Instantiate an Email options object and set its properties
CrystalDecisions.Shared.MicrosoftMailDestinationOptions myOptions;
myOptions = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions();
myOptions.UserName = UserName;
myOptions.Password = Password;
myOptions.MailSubject = Subject;
myOptions.MailMessage = Message;
myOptions.MailToList = MailTo;
myOptions.MailCCList = CCList;
//Assign the options object to the report
myExportOptions.DestinationOptions = myOptions;
myReport.Export(myExportOptions);
}

This procedure sets the report object’s destination type to be MicrosoftMail. Then it creates a new options variable and assigns the parameters to the appropriate properties. The last step assigns the options object to the DestinationOptions property of the report object and calls the Export() method.