vb.net - 无法在 VB.net 中将打印的 pdf 文档作为电子邮件附件附加
问题描述
我能够创建一个 pdf 文档,我可以浏览文件夹并毫无问题地打开文档。但是,当我的代码尝试将文件作为附件附加时,它会因此错误而失败,但路径和文件名是正确的。我怀疑该文件以某种方式打开,这会阻止附件。
文件证明:
抛出异常:mscorlib.dll 中的“System.IO.FileNotFoundException”找不到文件“C:\TEMP\1104280343081_INV0950.pdf”。这部分工作完美
Dim txtVarFile As String
Dim txtVarEmail As String = "test@test.com"
Dim txtVarPasss As String = "password"
Dim txtVarSMTP As String = "smtp.gmail.com"
Dim intVarPort As Integer = 587
Dim txtVarDescription As String
'Create Invoice and Save as pdf document
txtVarFile = "C:\TEMP\" & strClientNum & "_" & strInvNum & ".pdf"
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.PrinterSettings.DefaultPageSettings.Landscape = True
prtFrmInvoice.PrinterSettings = PageSetupDialog1.PrinterSettings
If prtFrmInvoice.PrinterSettings.IsValid Then
prtFrmInvoice.PrinterSettings.PrinterName = "Microsoft Print to PDF"
prtFrmInvoice.PrintFileName = txtVarFile
prtFrmInvoice.PrintAction = Printing.PrintAction.PrintToFile
prtFrmInvoice.Print()
End If
但这不起作用,它告诉我找不到文件,但文件在那里 在这一行抛出异常:eMail.From = New MailAddress(txtVarEmail)
'Send copy of Invoice per Email
Try
Dim SmtpServer As New SmtpClient()
Dim eMail As New MailMessage()
'Dim attachment As System.Net.Mail.Attachment
LogFile.Refresh()
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New Net.NetworkCredential(txtVarEmail, txtVarPasss)
SmtpServer.Port = intVarPort
SmtpServer.EnableSsl = True
SmtpServer.Host = txtVarSMTP
eMail = New MailMessage()
eMail.From = New MailAddress(txtVarEmail)
eMail.To.Add(strClientEmail)
eMail.Subject = "AltHealth Invoice"
eMail.Body = "Please find your latest invoice attached"
'attachment = New System.Net.Mail.Attachment(txtVarFile)
'eMail.Attachments.Add(attachment)
eMail.Attachments.Add(New Attachment(txtVarFile))
SmtpServer.Send(eMail)
MsgBox("The Invoice has been sent sucessfully via email - File: " & txtVarFile)
Catch ex As Exception
MsgBox("Send failure: " & ex.ToString())
End Try
解决方案
推荐阅读
- python - 找不到在命令行上运行的模块
- arduino - (Arduino)如何隔离色温值并打印成特定颜色?
- python - 7.zip 仅提取更新的文件
- php - PHP Curl API - 使用 curl 时远程服务器上的 Cookie 不起作用
- powerbi - 按 Power BI 中的最后一个非空白日期过滤
- python - 为什么为 numpy 函数选择名称“arange”?
- php - Laravel 5.5:安装 PHP 7.1.19 时找不到“整理器”
- jhipster - JHipster:通过 Okta 进行 Facebook 社交登录后的空白页面
- batch-file - 使用批处理脚本在文本文件中搜索和替换文本
- c++ - c++ libcurl - 使用前一个 curl 的响应作为另一个 curl 的参数