how to send the viewed crystal report in vb.net 2003
Printable View
how to send the viewed crystal report in vb.net 2003
The only way I know, is convert them to Image for example, and send it
i means any reports sent through mail in .pdf format
1.- Using Crystal Reports, process and export to pdf the desired rpt
2.- Using System.Net.Mail and MailMessage & SmtpClient, send mail with the pdf file as attachment
JG
Can you please provide some example
thanks
asm
Code:MPDFfile = "MyPDFfile.pdf"
MRPTfile = "MyRPTfile.rpt"
'Export rpt to pdf
CRReport = New CrystalDecisions.CrystalReports.Engine.ReportDocument
CRReport.Load(MRPTfile, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy)
For Each Module1.CRTable In CRReport.Database.Tables
CRTLI = CRTable.LogOnInfo
With CRTLI.ConnectionInfo
.ServerName = "MyServer"
.UserID = "user"
.Password = "password"
.DatabaseName = "DataBase"
End With
CRTable.ApplyLogOnInfo(CRTLI)
Next CRTable
Dim CRDiskFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions
Dim CRFormatTypeOptions As New CrystalDecisions.Shared.PdfRtfWordFormatOptions
CRDiskFileDestinationOptions.DiskFileName = MPDFFile
With CRReport.ExportOptions
.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
.DestinationOptions = CRDiskFileDestinationOptions
.FormatOptions = CRFormatTypeOptions
End With
CRReport.Export()
CRReport = Nothing
CRDiskFileDestinationOptions = Nothing
CRFormatTypeOptions = Nothing
'Send eMail with pdf as attachment
eMailFrom = "[email protected]" 'you need to have a mail service active in your pc
eMailTo = "[email protected]"
eMailSubject = "Subject"
eMailMessage = "You'll find an attachment within this mail & vbCrLf & vbCrLf & " "
eMailFile1 = MPDFfile
SendEmailMessage(eMailFrom, eMailTo, eMailSubject, eMailMessage, eMailFile1)
Maybe you need to declare some variablesCode:Public Sub SendEmailMessage(ByVal strFrom As String, _
ByVal strTo As String, _
ByVal strSubject As String, _
ByVal strMessage As String, _
ByVal file1 As String)
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo.Trim()))
With MailMsg
.BodyEncoding = System.Text.Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = False
Dim MsgAttach1 As New Attachment(file1)
MailMsg.Attachments.Add(MsgAttach1)
MsgAttach1 = Nothing
End With
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "smtp.mymailservice.com" 'email service active in your pc
SmtpMail.Send(MailMsg)
MailMsg = Nothing
SmtpMail = Nothing
End Sub
JG
Hi,
Which Class i import instead of
Import System.Net.Mail
Because in VB 2003, i did't found this class
asm
Hi,
I convert the source code from vb 2003 to vb 2008
and now the error message :
Failure sending mail
how to fix the error
thanks
asm
Repeat wich vb and cr version are you using and post the code
JG
VB 2008 and CR 11
Code:Public Function NativeEmailReport(ByVal rReportPath As String, ByVal rUser As String, ByVal rPassWord As String, ByVal rServer As String, ByVal rDataBase As String, ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMessage As String, ByVal rPdfFile As String, ByVal CrReportName As String, Optional ByVal CrSelectionFormula As String = "", Optional ByVal CrParameter As String = "") As Boolean
Dim IntCounter As Integer
Dim IntCounter1 As Integer
Dim CrConInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim ParamValue As New CrystalDecisions.Shared.ParameterDiscreteValue
Dim CurrValue As CrystalDecisions.Shared.ParameterValues
Dim CrSubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
Dim CrSubReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim StrParValPair() As String
Dim StrVal() As String
Dim Index As Integer
Try
CrReport.Load(rReportPath & CrReportName, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy)
CrReport.Refresh()
'---Parameters Checking
IntCounter = CrReport.DataDefinition.ParameterFields.Count
If IntCounter = 1 Then
If InStr(CrReport.DataDefinition.ParameterFields(0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
IntCounter = 0
End If
End If
'---Assign the Parameter value to there concurrent parameter fields
If IntCounter > 0 And Trim(CrParameter) <> "" Then
StrParValPair = CrParameter.Split("&")
For Index = 0 To UBound(StrParValPair)
If InStr(StrParValPair(Index), "=") > 0 Then
StrVal = StrParValPair(Index).Split("=")
ParamValue.Value = StrVal(1)
CurrValue = CrReport.DataDefinition.ParameterFields(StrVal(0)).CurrentValues
CurrValue.Add(ParamValue)
CrReport.DataDefinition.ParameterFields(StrVal(0)).ApplyCurrentValues(CurrValue)
End If
Next
End If
'---Connection Information to CrConInfo object for main report tables
CrConInfo.ConnectionInfo.UserID = rUser
CrConInfo.ConnectionInfo.Password = rPassWord
CrConInfo.ConnectionInfo.ServerName = rServer
CrConInfo.ConnectionInfo.DatabaseName = rDataBase
For IntCounter = 0 To CrReport.Database.Tables.Count - 1
'Database and tables name mapping
CrReport.Database.Tables(IntCounter).ApplyLogOnInfo(CrConInfo)
'Tables location mapping
CrReport.Database.Tables(IntCounter).Location = CrConInfo.ConnectionInfo.DatabaseName & ".dbo." & CrReport.Database.Tables(IntCounter).Location.Substring(CrReport.Database.Tables(IntCounter).Location.LastIndexOf(".") + 1)
Next
'---Checking for SubReport in Main Report and assign the Connection Information to Subreport tables
For Index = 0 To CrReport.ReportDefinition.Sections.Count - 1
For IntCounter = 0 To CrReport.ReportDefinition.Sections(Index).ReportObjects.Count - 1
With CrReport.ReportDefinition.Sections(Index)
If .ReportObjects(IntCounter).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
CrSubReportObject = CType(.ReportObjects(IntCounter), CrystalDecisions.CrystalReports.Engine.SubreportObject)
CrSubReport = CrSubReportObject.OpenSubreport(CrSubReportObject.SubreportName)
For IntCounter1 = 0 To CrSubReport.Database.Tables.Count - 1
'Database and tables name mapping
CrSubReport.Database.Tables(IntCounter1).ApplyLogOnInfo(CrConInfo)
'Tables location mapping
CrSubReport.Database.Tables(IntCounter1).Location = CrConInfo.ConnectionInfo.DatabaseName & ".dbo." & CrSubReport.Database.Tables(IntCounter1).Location.Substring(CrSubReport.Database.Tables(IntCounter1).Location.LastIndexOf(".") + 1)
Next
End If
End With
Next
Next
'---Selection Formula
If CrSelectionFormula.Length > 0 Then
CrReport.RecordSelectionFormula = CrSelectionFormula
End If
'------Export to PDF File
Dim CRDiskFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions
Dim CRFormatTypeOptions As New CrystalDecisions.Shared.PdfRtfWordFormatOptions
CRDiskFileDestinationOptions.DiskFileName = rPdfFile
With CrReport.ExportOptions
.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
.DestinationOptions = CRDiskFileDestinationOptions
.FormatOptions = CRFormatTypeOptions
End With
CrReport.Export()
CrReport = Nothing
CRDiskFileDestinationOptions = Nothing
CRFormatTypeOptions = Nothing
'--Send eMail with pdf as attachment
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo.Trim()))
Try
With MailMsg
.BodyEncoding = System.Text.Encoding.Default
.Subject = strSubject.Trim()
.Body = strMessage.Trim() & vbCrLf
.Priority = MailPriority.High
.IsBodyHtml = False
Dim MsgAttach1 As New Attachment(rPdfFile)
.Attachments.Add(MsgAttach1)
MsgAttach1 = Nothing
End With
Catch ex As Exception
Connect.SayMessage(ex.Message)
Exit Function
End Try
Dim SmtpMail As New SmtpClient
Try
SmtpMail.Host = Trim("smtp.gmail.com")
SmtpMail.EnableSsl = False
SmtpMail.Send(MailMsg)
Catch ex As Exception
Connect.SayMessage(ex.Message)
rPdfFile = Nothing
Exit Function
End Try
MailMsg = Nothing
SmtpMail = Nothing
rPdfFile = Nothing
Return True
Catch ex As System.Exception
Connect.SayMessage(ex.Message)
End Try
End Function