0

我能够创建一个 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
4

0 回答 0