Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

How to submit a record and send as an email attachment at once?

$
0
0
Greetings gurus,

I have a requirement to collect information from users by presenting a form to them.

Upon completing the form and hitting the submit button, the content of their submission is stored in the db and at the same time sent as an email attachment all at once.

Is this possible?

If yes, does anyone know what I need to modify in the code below?

The code is not fully cooked but I need some guidance on how to submit the record and at same time sending the contents of the record as an attachment via email.

Thanks for your awesome help.

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
 
        Dim table As New DataTable()
        Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("PersonnelPolicyDB").ConnectionString)
            Using cmd = New SqlCommand("usp_AddABCD", con)
                Using da = New SqlDataAdapter(cmd)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@oderno", "orderno.text")
                    cmd.Parameters.AddWithValue("@orderdate", "orderdate.text")
                    da.Fill(table)
                End Using
            End Using
        End Using
      End Sub
 

 
        Dim table As New DataTable()
        Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("PersonnelPolicyDB").ConnectionString)
            Using cmd = New SqlCommand("usp_GetABCD", con)
                Using da = New SqlDataAdapter(cmd)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@orderno", "orderno.text")
                   da.Fill(table)
                End Using
            End Using
        End Using
    End Sub
        
       Private Sub SendPDFEmail(dt As DataTable)
           Using sw As New StringWriter()
               Using hw As New HtmlTextWriter(sw)
                   Dim companyName As String = "my Company"
                   Dim orderNo As Integer = 2303
                   Dim sb As New StringBuilder()
                   sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>")
                   sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'>Order Sheet</td></tr>")
                   sb.Append("<tr><td colspan = '2'></td></tr>")
                   sb.Append("<tr><td>Order No:")
                   sb.Append(orderNo)
                   sb.Append("</td><td>Date: ")
                   sb.Append(DateTime.Now)
                   sb.Append("</td></tr>")
                   sb.Append("<tr><td colspan = '2'>Company Name : ")
                   sb.Append(companyName)
                   sb.Append("</td></tr>")
                   sb.Append("</table>")
                   sb.Append("
")
                   sb.Append("<table border = '1'>")
                   sb.Append("<tr>")
                   For Each column As DataColumn In dt.Columns
                       sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>")
                       sb.Append(column.ColumnName)
                       sb.Append("</th>")
                   Next
                   sb.Append("</tr>")
                   For Each row As DataRow In dt.Rows
                       sb.Append("<tr>")
                       For Each column As DataColumn In dt.Columns
                           sb.Append("<td>")
                           sb.Append(row(column))
                           sb.Append("</td>")
                       Next
                       sb.Append("</tr>")
                   Next
                   sb.Append("</table>")
                   Dim sr As New StringReader(sb.ToString())
   
                   Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
                   Dim htmlparser As New HTMLWorker(pdfDoc)
                   Using memoryStream As New MemoryStream()
                       Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
                       pdfDoc.Open()
                       htmlparser.Parse(sr)
                       pdfDoc.Close()
                       Dim bytes As Byte() = memoryStream.ToArray()
                       memoryStream.Close()
   
                       Dim mm As New MailMessage("NoReply@domain.com", "myemail@domain.com")
                       mm.Subject = "iTextSharp PDF"
                       mm.Body = "iTextSharp PDF Attachment"
                       mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "iTextSharpPDF.pdf"))
                       mm.IsBodyHtml = True
                       Dim smtp As New SmtpClient()
                       smtp.Host = "myhostname"
                       smtp.EnableSsl = False
                       Dim NetworkCred As New System.Net.NetworkCredential()
                       NetworkCred.UserName = "myusername"
                       NetworkCred.Password = "mypassword"
                       smtp.UseDefaultCredentials = True
                       smtp.Credentials = NetworkCred
                       smtp.Port = 25
                       smtp.Send(mm)
                   End Using
               End Using
           End Using
    End Sub

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>