I'm migrating web servers and rewriting some of our applications (vb.net environment). In doing so, I have come across a dilema, and would greatly appreciate some help.
I can successfully pull a .pdf file out of a database and display it:
Dim iID As Integer
Dim bPDF As Byte()
iID = Convert.ToInt32(Request.QueryString("Id"))
bPDF = CodeMod.GetPdfBytes(iID)
Response.ContentType = "application/pdf"
Response.OutputStream.Write(bPDF, 0, bPDF.Length)
---------------------------------
Public Function GetPdfBytes(ByVal id As Int32) As Byte()
Dim SQLcon As New SqlClient.SqlConnection
Dim SQLcmd As New SqlClient.SqlCommand
Dim dt As New DataTable
SQLcon.ConnectionString = sConnection("pdfStore")
SQLcmd.CommandText = "[DOIMSQL,1433].[DOI_PDF_Storage].dbo.sel_pdf_id"
SQLcmd.CommandType = CommandType.StoredProcedure
SQLcmd.Parameters.Add("@pdf_id", SqlDbType.VarChar).Value = id
SQLcmd.Connection = SQLcon
SQLcon.Open()
dt.Load(SQLcmd.ExecuteReader)
SQLcon.Close()
GetPdfBytes = DirectCast(dt.Rows(0)("pdf_file"), Byte())
End Function
AND I can successfully send an email with a file attachment:
CodeMod.sendMail(strTo, strFrom, "Test Email", "Test Content",, Server.MapPath("/Test/TEST.pdf"))
---------------------------------
Public Sub sendMail(strTo As String, strFrom As String, strSubject As String, strMsg As String, Optional ByVal strCc As String = "", Optional ByVal file As String = "")
Dim oMMsg As New Net.Mail.MailMessage()
oMMsg.From = New MailAddress(strFrom)
oMMsg.To.Add(strTo)
oMMsg.Subject = strSubject
oMMsg.Body = strMsg
oMMsg.IsBodyHtml = True
If file <> "" Then oMMsg.Attachments.Add(New Attachment(file))
Dim client As New SmtpClient()
client.Host = "correct.information"
client.Port = 25
client.Send(oMMsg)
End Sub
What I need to do is to marry these two capabilities. I need to retrieve a .pdf from out of the database and send it as an email attachment...
...without ever writing the file to the file structure.