gpt4 book ai didi

c# - 从字节数组中获取要作为附件发送的文件

转载 作者:太空狗 更新时间:2023-10-29 21:04:48 25 4
gpt4 key购买 nike

我有一个 ASP.NET MVC 应用程序,它必须向收件人列表发送一封电子邮件,其中包含详细说明特定“项目”的附件。我使用 SQL Server Reporting Services (SSRS) 从报告中获取了这方面的详细信息。

我以前从未真正使用过 SSRS,但我从一位使用它的同事那里收到了一些代码。他对报告所做的是在浏览器中将其下载到客户的机器上。因此,如果我不这样做,我会使用包含报告数据的字节数组。

有没有一种方法可以将其作为附件发送,而无需先将文件物理写入服务器的文件系统?该报告将采用 excel 格式或 pdf 格式。

编辑:我正在使用 SmtpClient 发送电子邮件。

最佳答案

获取byte[]中的文件数据

byte[] binaryFile = //  get your file data from the SSRS ...
string filename = "SSRS.pdf";

准备目标地址的列表或数组:

string[] addresses = // get addresses somehow (db/hardcoded/config/...)

通过SmtpClient发送smtp消息:

MailMessage mailMessage= new MailMessage();

mailMessage.From = new MailAddress("sender email address goes here");

// Loop all your clients addresses
foreach (string address in addresses)
{
mailMessage.To.Add(address);
}

mailMessage.Subject = "your message subject goes here";
mailMessage.Body = "your message body goes here";

MemoryStream memoryStream = new MemoryStream(binaryFile);
mailMessage.Attachments.Add( new Attachment( memoryStream, filename , MediaTypeNames.Application.Pdf ));

SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);

关于c# - 从字节数组中获取要作为附件发送的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com