Of course.
Here is my solution and you can feel free to make it better and more efficient.
I used small HTML sample data.
We used Multiview control to create two views.
One view to datatEntry and the other view prView to preview data.
First create sample HTML with just name, title, email address.
Then on preview page, we preview the data entered on dataEntry view:
<%--BEGIN: This part is streamed into PDF and sent as attachment to email on the fly--%>
<div id = "EmailCopy">
<table style="width:100%" class="table">
<tr>
<td style="margin-left:20px;"><span style="font-weight:bold;font-size:12px;">Employee Name:</span>
<asp:Label ID="lblEmpName" runat="server" />
</td>
<td><span style="font-weight:bold;font-size:12px;">Title:</span>
<asp:Label ID="lblPreviewTitle" runat="server" /></td>
<td><span style="font-weight:bold;font-size:12px;">Email:</span>
<asp:Label ID="lblPreviewEmail" runat="server" /></td>
<td><span style="font-weight:bold;font-size:12px;">Badge ID:</span>
<asp:Label ID="lblPreviewEmpID" runat="server" /></td>
</tr>
</table>
</div>
<%--END: This part is streamed into PDF and sent as attachment to email on the fly--%>
<asp:HiddenField ID="hfcopydHtml" ClientIDMode="Static" ViewStateMode="Enabled" runat="server" />
<script src="js/core.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnSave]").click(function () {
$("[id*=hfcopydHtml]").val($("#EmailCopy").html());
});
});
</script>
On the preview page (btnNext), we store values of the three form fields, name, title, email to label control IDs:
Session("badgenumber") = txtEmpID.Text
Session("empname") = txteName.Text
Session("etitle") = txttitle.Text
Session("email") = txtemail.Text
Session("edob") = txtdob.Text
lblEmpName.Text = Session("empname")
lblPreviewTitle.Text = Session("etitle")
lblPreviewEmail.Text = Session("email")
lblPreviewEmpID.Text = Session("badgenumber") & Session("edob")
lblSign.Text = Session("empname")
To add these data to the body of email, we create a FillSummary method:
'everything inside the FillSummary method is for previewing user input data on Preview page before submission.
Private Sub FillSummary()
'//Show employee summary info
Session("badgenumber") = txtEmpID.Text
Session("empname") = txteName.Text
Session("etitle") = txttitle.Text
Session("email") = txtemail.Text
Session("edob") = txtdob.Text
lblEmpName.Text = Session("empname")
lblPreviewTitle.Text = Session("etitle")
lblPreviewEmail.Text = Session("email")
lblPreviewEmpID.Text = Session("badgenumber") & Session("edob")
lblSign.Text = Session("empname")
End Sub
Finally, the sendEmail (). Did my best to comment everything each section does:
Protected Sub SendEmail()
Dim Conn As SqlConnection
'Read in connection String
Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("constring").ConnectionString)
Conn.Open()
'We want to make sure user info is successfully submitted into the DB. So, we grab sender email from DB instead of form field.
Dim scmd As SqlCommand = New SqlCommand("Select email from Employees where empID = @PreviewEmpID", Conn)
scmd.Parameters.AddWithValue("@PreviewEmpID", lblPreviewEmpID.Text)
scmd.CommandType = CommandType.Text
Dim validEmail As Boolean = False
''*** The Fastest way to get Single Row Data ***
Dim reader As SqlDataReader = scmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
sEmail = reader.GetString(0)
'if we are here then something got returned.
'so probably a valid email.
validEmail = True
End If
Dim fileName As String = lblEmpName.Text & DateTime.Now.Ticks & ".pdf"
Dim filePath As String = Server.MapPath(String.Format("~/PDF/{0}", fileName))--we store each pdf in PDF folder
Using stream As New FileStream(filePath, FileMode.Create)
'Logo to be affixed at top of PDF sent via email as attachment. Thank you Dharmendra for this code
Dim html As String = "<img style='background-color:black' src='" & GetUrl("images/logo.png") & "' alt='ASPSnippets.com' /><br /><br /><br />"
html += Request.Form(hfcopydHtml.UniqueID)
Dim sr As New StringReader(html)
Dim pdfDoc As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
stream.Close()
End Using
'//Ready email message to recepient
'Create the msg object to be sent
Dim msg As New MailMessage()
'Let's ready image to be sent and displayed as logo.
'first grab as an attachment. This logo is displayed on the body of email
Dim inlineLogo As New Attachment("C:\inetpub\wwwroot\Disclaimer" & System.DateTime.Now.ToString("yyyy") - 1 & "\images\logo.png")
msg.Attachments.Add(inlineLogo)
Dim contentID As String = "Image"
inlineLogo.ContentId = contentID
'To make the image display as inline and not as attachment. Had to go this route due to security issues
inlineLogo.ContentDisposition.Inline = True
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline
'Add your email address to the recipients
msg.[To].Add(sEmail)
'Configure the address we are sending the mail from
Dim EmailSender As New MailAddress("no-reply@myemail.com")
msg.From = EmailSender
'Append their name in the beginning of the subject
msg.Subject = "Disclaimer Form Completion"
'To embed image in email.
msg.Body = (Convert.ToString("<htm><body> <img src=""cid:") & contentID) & """> </body></html><br />" & body
msg.Attachments.Add(New Attachment(filePath))
msg.IsBodyHtml = True
'Configure an SmtpClient to send the mail.
Dim client As New SmtpClient("smtp.emailrelay")
client.EnableSsl = False
'//Display completed Disclaimer to employee
FillSummary()
'Send the msg
client.Send(msg)
End Sub