SelectPdf for .NET - Html to Pdf Converter - Convert And Email as Attachment - VB.NET / ASP.NET MVC Sample

This sample shows the simplest code that can be used To convert an url To pdf Using SelectPdf Pdf Library For .NET And Then email the generated PDF document As an attachment.

IMPORTANT: Remember to set the SMTP server details in web.config.

Url :



Sample Code VB.NET

Imports SelectPdf
Imports System.IO
Imports System.Net.Mail

Namespace Controllers
    Public Class ConvertAndEmailController
        Inherits Controller

        ' GET: ConvertAndEmail
        Function Index() As ActionResult
            Return View()
        End Function

        Public Function Index(fields As FormCollection) As ActionResult
            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

                ' create a new pdf document converting an url
                Dim doc As PdfDocument = converter.ConvertUrl(fields("TxtUrl"))

                ' create memory stream to save PDF
                Dim pdfStream As New MemoryStream()

                ' save pdf document into a MemoryStream

                ' reset stream position
                pdfStream.Position = 0

                ' create email message
                Dim message As New MailMessage()
                message.From = New MailAddress("")
                message.[To].Add(New MailAddress(fields("TxtEmail")))
                message.Subject = "SelectPdf Sample - Convert and Email as Attachment"
                message.Body = "This email should have attached the PDF document " +
                    "resulted from the conversion of the following url to pdf: " +
                message.Attachments.Add(New Attachment(pdfStream, "Document.pdf"))

                ' send email
                Dim smtp = New SmtpClient()

                ' close pdf document

                ViewData("Message") = "Email sent"
            Catch ex As Exception
                ViewData("Message") = "An error occurred: " + ex.Message
            End Try

            Return View()
        End Function
    End Class
End Namespace