c# – 从字节数组中获取要作为附件发送的文件
发布时间:2020-12-15 18:09:21 所属栏目:百科 来源:网络整理
导读:我有一个ASP.NET MVC应用程序,它必须向收件人列表发送一封电子邮件,其中附件详细说明了特定的“项目”.我通过使用SQL Server Reporting Services(SSRS)从报告中获取详细信息. 我之前从未真正使用过SSRS,但是我收到了他使用它的同事的一些代码.他对报告的处理
我有一个ASP.NET MVC应用程序,它必须向收件人列表发送一封电子邮件,其中附件详细说明了特定的“项目”.我通过使用SQL Server Reporting Services(SSRS)从报告中获取详细信息.
我之前从未真正使用过SSRS,但是我收到了他使用它的同事的一些代码.他对报告的处理是将其在浏览器中下载到客户端的计算机上.所以,如果我不这样做,我会坐在一个包含报告数据的字节数组中. 有没有一种方法可以将此作为附件发送而无需先将文件物理写入服务器的文件系统?该报告将采用excel格式或pdf格式. 编辑:我正在使用SmtpClient发送电子邮件. 解决方法
要做到这一点,您需要利用SSRS ReportManager API,如下所示.
>首先使用SSRS从Web服务的报告中读取 Reporting services: Get the PDF of a generated report 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发送方法. 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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |