1

我想将存储为 SQL Server 中二进制对象的 PDF 文件附加到电子邮件中,但不在磁盘上创建(临时)文件。

byte[]我已经有了从 SQL Server 中的二进制字段中提取 PDF 文件作为数组的第一步。

我也有设置的步骤MailMessage

MailMessage aMailMessage = new MailMessage();
// set address, subject, body
aMailMessage.Attachments.Add(attachment);

我被卡住的地方是attachment对象:

Attachment attachment = new Attachment(... what goes here? ...);

的构造函数Attachment主要接受 a string fileName(我没有也不想要)或 a System.IO.Stream contentStream

所以我的问题是:使用给定的byte[]数组是否可以Attachment在磁盘上没有中间文件的情况下创建正确的对象,我必须执行哪些步骤?

先感谢您!

4

1 回答 1

2

您可以将 byte[] 转换为MemoryStream并从中创建Attachment实例。这是一个例子:

ContentType ct = new ContentType(MediaTypeNames.Application.Pdf);
MemoryStream pdf = new MemoryStream(pdfContent);
Attachment data = new Attachment(pdf, ct);
于 2010-06-11T12:26:55.280 回答