2

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.

Bob Cooper
  • 31
  • 3
  • Follow [Attach a file from MemoryStream to a MailMessage in C#](http://stackoverflow.com/q/5336239/205233) but don't forget to "rewind" your stream to position 0. – Filburt Apr 25 '17 at 16:07

1 Answers1

0

If I get it right, you can write that PDF into a memory stream and make an attachment directly from the memory stream. There is an existing separate question on how to attach from memory stream

Attach a file from MemoryStream to a MailMessage in C#

Community
  • 1
  • 1
Serge Makarov
  • 351
  • 2
  • 7
  • Took a bit to translate it from C# over to VB.Net, but ultimately this pointed me in the right direction. Thanks Serge. – Bob Cooper Apr 25 '17 at 19:55