[英]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.我在互联网上找到了可以通过 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谢谢
I think the problem is your pdf attachment file.我认为问题在于您的 pdf 附件文件。 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 object 导出到 pdf 文件:
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, pdfFile)
If no exception occurs your attachment is created and ready to be mailed.如果没有发生异常,您的附件已创建并准备好邮寄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.