Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...