Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
950 views
in Technique[技术] by (71.8m points)

crystal reports - Sending email with attachment using VB.net

I know this is an old issue or concern but can you help me on this.

I have a source code found in the internet that can send an attachment via email. I tried it in my application but I getting this error.

System.Web.HttpException (0x8000405) Invalid mail attachment C:File.pdf  
at System.Web.Mail.MailAttachment.VerifyFile() 
at System.Web.Mail.Attachment..ctor(String filename)

This is the code I found

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Web.Mail
Public Class ReportsForm
    Dim cryRpt As New ReportDocument
    Dim pdfFile As String = "C:File.pdf"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        cryRpt.Load("Crystal Report Path here")
        CrystalReportViewer1.ReportSource = cryRpt
        CrystalReportViewer1.Refresh()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New  _
            DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions
            CrDiskFileDestinationOptions.DiskFileName = pdfFile
            CrExportOptions = cryRpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            cryRpt.Export()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sendMail()
    End Sub
    Private Sub sendMail()
        Try
            Dim Smtp As SmtpMail
            SmtpMail.SmtpServer.Insert(0, "hostname")
            Dim Msg As MailMessage = New MailMessage
            Msg.To = "to address"
            Msg.From = "from address"
            Msg.Subject = "Crystal Report Attachment "
            Msg.Body = "Crystal Report Attachment "
            Msg.Attachments.Add(New MailAttachment(pdfFile))
            SmtpMail.Send(Msg)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Thank you

question from:https://stackoverflow.com/questions/65886531/sending-email-with-attachment-using-vb-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the problem is your pdf attachment file. You are going to need somewhere safe to create it. You'll create the attachment file path and name using this code:

Dim pdfFile As String = System.IO.Path.GetTempPath & "File.pdf"
If System.IO.File.Exists(pdfFile) Then My.Computer.FileSystem.DeleteFile(pdfFile)

Then you'll export the cryRpt object to a pdf file using just one line of code:

cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, pdfFile)

If no exception occurs your attachment is created and ready to be mailed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...