Greetings again expert helpers.
I found once again, a great article about generating pdf in memory and sending as an email attachment. The article is here:
https://www.aspsnippets.com/Articles/iTextSharp-Generate-PDF-in-Memory-and-send-as-Email-Attachment-using-C-VBNet-and-ASPNet.aspx
It works great.However, since it is using htmlWorker, it makes it difficult to format the page and use css to meet our specs.
As a result, I attempted to convert the code from that link to the xmlWorker.
It inserts records into the database successfully.
However, it is not sending the email and of course no PDF attachment.
Any ideas what I am doing wrong?
Sorry for the long code below:
Imports iTextSharp.tool.xml
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Net
Imports System.Net.Mail
Imports iTextSharp.text.html.simpleparser
Imports System.Data.SqlClient
Imports System.Runtime.CompilerServices
Partial Class _Default
Inherits System.Web.UI.Page
Private strConn As String = ConfigurationManager.ConnectionStrings("constr").ToString()
Private sqlCon As SqlConnection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
End If
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
System.Threading.Thread.Sleep(5000)
Try
Dim table As New DataTable()
Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd = New SqlCommand("INSERT INTO Policies(userid,entrydate,signature, email) VALUES(@empID, @lDate,@signature, @email)", con)
Using da = New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@empID", txtUserName.Text)
cmd.Parameters.AddWithValue("@lDate", txtDate.Text)
cmd.Parameters.AddWithValue("@signature", txtSignature.Text)
cmd.Parameters.AddWithValue("@email", txtEmail.Text)
da.Fill(table)
End Using
End Using
End Using
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(0) {New DataColumn(" ")})
' dt.Rows.Add(txtaffirmation.Text)
SendPDFEmail(dt)
'If the message failed at some point, let the user know
Label1.ForeColor = System.Drawing.Color.Blue
Label1.Text = "Success!<br> A copy of the completed form has been sent to your inbox. If you do not receive an email within 5 minutes (usually immediately), check your spam box."
'Reset markup controls
txtFullName.Text = ""
txtUserName.Text = ""
txtSignature.Text = ""
txtDate.Text = ""
txtEmail.Text = ""
Catch ex As Exception
'If the message failed at some point, let the user know
Label1.ForeColor = System.Drawing.Color.Red
Label1.Text = ex.Message
End Try
''reset markup controls
'ClearTextBoxes(Page)
End Sub
Private Sub SendPDFEmail(ByVal dtb As DataTable)
Dim strHtml As String
Dim memStream As New MemoryStream()
Dim strWriter As New StringWriter()
Dim FullName As String = txtFullName.Text
Dim user As String = txtUserName.Text
Dim signedName As String = txtSignature.Text
Dim lDate As String = txtDate.Text
Dim body As String = "<br />"
body = body & "<table style=""background-color:#F8F8F8"" cellspadding=""2"" border=""0"" width=""100%"" >"
For Each column As DataColumn In dtb.Columns
body = body & "<td style=""color: black"" align=""Left"">" & txtaffirmation.Text & "</td></tr></table><br>"
Next
body = body & " <br><br>"
body = body & "<table style=""background-color:#F8F8F8"" cellspadding=""2"" border=""0"" width=""100%"" >"
body = body & "<tr><td align=""left""><u>" & FullName & "</u></td>"
body = body & "<td align=""left""><u>" & user & "</u></td></tr>"
body = body & "<tr><td align=""left"">Employee Name</td>"
body = body & "<td align=""left"">Employee ID</td></tr>"
body = body & "<tr><td style=""font-family:Brush Script Std"" align=""left""><u><i>" & signedName & "</i> </u></td><td align=""left"">" & " " & "</td>"
body = body & "<td align=""left"">Employee Signature</td><td align=""left"">" & " " & "</td></tr>"
body = body & "<tr><td align=""left""><u>" & lDate & "</u></td><td align=""left"">" & " " & "</td></tr>"
body = body & "<td align=""left"">Date</td><td align=""left"">" & " " & "</td></tr></table>"
strHtml = strWriter.ToString()
strWriter.Close()
strWriter.Dispose()
Dim strFileShortName As String = "test" & DateTime.Now.Ticks & ".pdf"
Dim strFileName As String = HttpContext.Current.Server.MapPath("reports\" & strFileShortName)
Dim docWorkingDocument As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 1, 1, 0, 0)
Dim srdDocToString As StringReader = Nothing
Try
Dim pdfWrite As PdfWriter
pdfWrite = PdfWriter.GetInstance(docWorkingDocument, New FileStream(strFileName, FileMode.Create))
srdDocToString = New StringReader(strHtml)
docWorkingDocument.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString)
Catch ex As Exception
Response.Write(ex.Message)
Finally
If Not docWorkingDocument Is Nothing Then
docWorkingDocument.Close()
docWorkingDocument.Dispose()
End If
If Not srdDocToString Is Nothing Then
srdDocToString.Close()
srdDocToString.Dispose()
End If
End Try
Using memoryStream As New MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(docWorkingDocument, memoryStream)
docWorkingDocument.Open()
'insert image into pdf
Dim logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/fc3.png"))
logo.SetAbsolutePosition(29, 680) ' insert top left
'logo.ScaleToFit(350.0F, 300.0F) ' reduce image size
docWorkingDocument.Add(logo)
docWorkingDocument.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Dim mm As New MailMessage("NoReply@email.com", txtEmail.Text)
mm.Subject = "From: " & txtFullName.Text
mm.Body = "Please see attached copy of your completed form. "
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), user & ".pdf"))
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = False
Dim NetworkCred As New System.Net.NetworkCredential()
NetworkCred.UserName = "myemail@gmail.com"
NetworkCred.Password = "mpassword"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Using
End Sub
End Class