Email Crystal Report as PDF When Viewed

C

crjunk

Currently I have a Crystal Report that automatically loads as a PDF in
the browser whenever the user views the report. This is working great.
What I would like to do is add code that automatically email this
report to a specific address whenever the report is viewed. Is this
possible? Here is the code that I'm currently using to view the report
as a PDF.

Private Sub LoadReport()

If Session("LoggedIn") = True Then

Dim tmpFileName As String
Dim myDataSet As New DataSet
Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
Dim FName, MyName As String

tmpFileName = Trim(Request.QueryString("fn"))

tmpFileName = Trim(tmpFileName) & ".xml"

CRViewer.Visible = True

oRpt.Load(Server.MapPath(".") & "\MPRInvoice.rpt")

'Read the XML file using the ReadXml method
myDataSet.ReadXml("C:\XML\" & Trim(tmpFileName))

oRpt.SetDataSource(myDataSet)

oRpt.ExportOptions.ExportDestinationType =
CrystalDecisions.[Shared].ExportDestinationType.DiskFile
oRpt.ExportOptions.ExportFormatType =
CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

FName = "C:\XML\" & Session.SessionID.ToString.Trim &
".pdf"
DiskOpts.DiskFileName = "C:\XML\" &
Session.SessionID.ToString.Trim & ".pdf"

MyName = DiskOpts.DiskFileName
oRpt.ExportOptions.DestinationOptions = DiskOpts
oRpt.Export()

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(FName)
Response.Flush()
Response.Close()
System.IO.File.Delete(FName)
Else
Response.Write("<font face='Arial, Helvetica,
sans-serif'><div align='center'><b>You are no longer logged into the
system.</b></div></font>")
End If

End Sub


I thought there might be a temporary PDF file in the C:\XML\ directory,
but no such luck. If a physical pdf file is being created in this
directory, I cannot see it before it gets deleted.

Thanks,
CR Junk
 
C

crjunk

Thanks for the info MD. It turned out to be a lot easier that I
thought it would. Here is what I did for those interested:

'----------- create physical pdf --------

oRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,
"C:\XML\" & Trim(Request.QueryString("fn")) & ".pdf")

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(FName)
Response.Flush()
Response.Close()
System.IO.File.Delete(FName)

'------------- send mail begin ---------------
Dim email = New MailMessage
email.BodyFormat = MailFormat.Text
Dim attach = New MailAttachment("C:\XML24\" &
Trim(Request.QueryString("fn")) & ".pdf")


email.From = "(e-mail address removed)"
email.To = "(e-mail address removed)"
email.Subject = "E-MAIL TEST"
email.Body = "This is an e-mail test. Sending e-mail
through asp.net"
email.Attachments.Add(attach)
' Set the SMTP server and send the email
SmtpMail.SmtpServer = "192.168.000.00"
SmtpMail.Send(email)
'------------- send mail end ---------------



Thanks again!
CR Junk
 
C

crjunk

Thanks for the info MD. It turned out to be a lot easier that I
thought it would. Here is what I did for those interested:

'----------- create physical pdf --------

oRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,
"C:\XML\" & Trim(Request.QueryString("fn")) & ".pdf")

Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(FName)
Response.Flush()
Response.Close()
System.IO.File.Delete(FName)

'------------- send mail begin ---------------
Dim email = New MailMessage
email.BodyFormat = MailFormat.Text
Dim attach = New MailAttachment("C:\XML\" &
Trim(Request.QueryString("fn")) & ".pdf")


email.From = "(e-mail address removed)"
email.To = "(e-mail address removed)"
email.Subject = "E-MAIL TEST"
email.Body = "This is an e-mail test. Sending e-mail
through asp.net"
email.Attachments.Add(attach)
' Set the SMTP server and send the email
SmtpMail.SmtpServer = "192.168.000.00"
SmtpMail.Send(email)
'------------- send mail end ---------------



Thanks again!
CR Junk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top