我如何从数据库中提取.pdf,然后将其插入电子邮件中,而无需将其写入文件结构



我正在迁移Web服务器并重写我们的某些应用程序(vb.net环境(。这样一来,我遇到了一个障碍,非常感谢您的帮助。

我可以成功地将.pdf文件从数据库中抽出并显示:

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

我可以成功发送带有文件附件的电子邮件:

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

我需要做的是嫁给这两个功能。我需要从数据库中检索.pdf并将其作为电子邮件附件发送...

...不从未将文件写入文件结构。

如果我正确得出,则可以将PDF写入内存流并直接从内存流制作附件。关于如何从内存流附加

有一个现有的单独问题

在C#

中将文件从内存流附加到mailMessage

最新更新