【问题标题】:Get file to send as attachment from byte array从字节数组获取要作为附件发送的文件
【发布时间】:2014-06-06 09:23:27
【问题描述】:

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

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

有没有一种方法可以在不先将文件物理写入服务器的文件系统的情况下将其作为附件发送?报告将采用 excel 格式或 pdf 格式。

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

【问题讨论】:

  • 您有带有文件上传功能的 ASP.NET MVC(版本?)Web 服务器,并且您想通过电子邮件(SMTP?)将您上传的文件发送到电子邮件地址列表?
  • @JossefHarush:不,我没有文件上传控件。我要附加的文件来自从 SQL 报告服务器检索的 SSRS 报告。我可以在代码中配置以检索各种格式的报告,但我将使用 xsl 或 pdf。
  • 谁将文件保存到文件系统?您还是您从同事那里得到的“黑盒”解决方案?
  • @JossefHarush:这听起来有点像不友好的话。谁说我将文件保存到文件系统?我发现我可以从byte[] 创建一个流,并使用该流和适当的内容类型创建一个Net.Mail.Attachment。我会尽快将其发布为我的解决方案。

标签: c# asp.net reporting-services


【解决方案1】:

为此,您需要利用 SSRS ReportManager API,如下所示。

  1. 首先阅读来自使用 SSRS 的 Web 服务的报告
  2. 将文件读入内存而不是保存到服务器或客户端
  3. 将 MemoryStream 对象直接发送到电子邮件服务器。

Reporting services: Get the PDF of a generated report How to send an email with attachments using SmtpClient.SendAsync?

string strReportUser = "RSUserName";
string strReportUserPW = "MySecretPassword";
string strReportUserDomain = "DomainName";

string sTargetURL = "http://SqlServer/ReportServer?" +
   "/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" +
   ParamValue;

HttpWebRequest req =
      (HttpWebRequest)WebRequest.Create( sTargetURL );
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential(
    strReportUser,
    strReportUserPW,
    strReportUserDomain );

HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();

Stream fStream = HttpWResp.GetResponseStream();

HttpWResp.Close();



//Now turn around and send this as the response..
ReadFullyAndSend( fStream );

ReadFullyAnd 发送方法。 注意:SendAsync 调用,因此您无需等待服务器完全发送电子邮件,然后再将用户带回点头之地。

public static void ReadFullyAndSend( Stream input )
{
   using ( MemoryStream ms = new MemoryStream() )
   {
      input.CopyTo( ms );

        MailMessage message = new MailMessage("from@foo.com", "too@foo.com");
            Attachment attachment = new Attachment(ms, "my attachment",, "application/vnd.ms-excel");
            message.Attachments.Add(attachment);
            message.Body = "This is an async test.";

            SmtpClient smtp = new SmtpClient("localhost");
            smtp.Credentials = new NetworkCredential("foo", "bar");
            smtp.SendAsync(message, null);
   }
}  

【讨论】:

  • 谢谢。这与我找到的解决方案非常相似。我在处理它时确实注意到我必须设置附件的内容类型,例如,如果它是 xls:mail.Attachments.Add(new Attachment(stream, "Research - " + projectId + ".xls" , "application/vnd.ms-excel"));
  • 啊,是 mime 类型,我把它加到上面,这样万一有人看到并使用上面的代码,它是可靠的。
【解决方案2】:

获取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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    相关资源
    最近更新 更多