如何将创建的PDF作为附件发送?

时间:2014-09-17 19:02:12

标签: asp.net pdf

这是我的代码:

    Dim doc As Document = New Document
    PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\1.pdf", FileMode.Create))
    doc.Open()
    doc.Add(New Paragraph("REQESTOR"))
    doc.Add(New Paragraph("Joe Shmoe"))
    doc.Close()

    Dim smtp As New SmtpClient("SMTP.com")
    Dim mail As New MailMessage("ESHM@cgn.com", "Joe.Shmoe@ncgn.com", "Test", "Hi")

    Dim attachment As System.Net.Mail.Attachment
    attachment = New System.Net.Mail.Attachment("~/1.pdf")
    mail.Attachments.Add(attachment)


    Try
        smtp.Send(mail)
    Catch ex As Exception
        Dim ss As String
        ss = ex.Message
    End Try

我创建了一个新的pdf文档然后我尝试将该pdf文档作为电子邮件中的附件发送。但是,当我运行此代码时,我收到一个错误: 找不到路径'C:\ Program Files(x86)\ IIS Express \〜\ 1.pdf'的一部分 无论如何要将创建的pdf文档作为附件发送到电子邮件???

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

显然C:\Program Files (x86)\IIS Express\~\1.pdf不是有效路径。将您的代码更改为:

attachment = New System.Net.Mail.Attachment(Server.MapPath("~/1.pdf"))

.NET将以这种方式为您解析文件的路径(假设您的站点根目录中实际上有一个名为1.pdf的文件)。